Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 控制器实例变量未在JS视图中呈现_Ruby On Rails_Ruby_Ruby On Rails 5 - Fatal编程技术网

Ruby on rails 控制器实例变量未在JS视图中呈现

Ruby on rails 控制器实例变量未在JS视图中呈现,ruby-on-rails,ruby,ruby-on-rails-5,Ruby On Rails,Ruby,Ruby On Rails 5,我有一个控制器: class CommentariesController < ApplicationController before_action :authenticate_user! before_action :find_commentary, only: [:update, :destroy] before_action :find_commentable, only: [:create] def create @commentary = @comm

我有一个控制器:

class CommentariesController < ApplicationController

  before_action :authenticate_user!
  before_action :find_commentary, only: [:update, :destroy]
  before_action :find_commentable, only: [:create]

  def create
    @commentary = @commentable.commentaries.new(commentary_params)
    @commentary.user_id = current_user.id
    @commentary.save!
  end

  def update
    @commentary.update(commentary_params) if current_user.authorized_for?(@commentary)
  end

  def destroy
    @commentary.destroy  if current_user.authorized_for?(@commentary)
  end

  private

  def find_commentary
    @commentary = Commentary.find(params[:id])
  end

  def commentary_params
    params.require(:commentary).permit(:body)
  end

  def find_commentable
    if params[:question_id]
      @commentable = Question.find(params[:question_id])
    elsif params[:answer_id]
      @commentable = Answer.find(params[:answer_id])
    end
  end
end
\u commentation.html.slim

div class='d-flex justify-content-end mb-2'
  div class='card col-10'
    div class='card-body small text-muted' = commentary.body

当处理
create
操作时,一切正常,正在创建注释,并成功保存到数据库中,但是jQuery没有添加注释html代码,我没有收到任何错误,但是这样的响应:

$("#-").append("")

我不知道这里发生了什么,所以我没有错误,注释被保存,但是我得到了空的注释对象字段

在ERB中,
之间有一个区别,第一个不输出任何东西,第二个输出任何东西

$("#<% @commentary.commentable_type.underscore %>-<% @commentary.id %>")
$(“#-”)
应该是:

$("#<%= @commentary.commentable_type.underscore %>-<%= @commentary.id %>")
$(“#-”)
$("#<%= @commentary.commentable_type.underscore %>-<%= @commentary.id %>")