繁体   English   中英

Rails-将ajax添加到局部,未定义的方法“渲染”

[英]Rails - adding ajax to partials, 'undefined method `render''

我在ID为“ chapcomments”的div内有一个带有提交标签的局部表格-

<%= f.submit "Post", remote: true %>

在正确的视图文件夹中,我有create.js.erb-

$('#chapcomments').html("<%=j render 'shared/chap_comment_complete' %>");

在控制器中,我有一个格式块,其中包括

def create
  @chap_comment = current_user.chap_comments.build(chap_comment_params)

  respond_to do |format|
    if @chap_comment.save
      format.js
      format.html {redirect_to chapter_path(@chap_comment.chapter)}
    else
      format.html { render :new }
      format.json { render json: @chap_comment.errors, status: :unprocessable_entity }
    end
  end
end

...但是我没有得到ajax的行为...

我要去哪里错了?

您已经进行了更改,以使JS位于视图中正确的js.erb文件中,但是您需要将remote: true部分从Submit标签移到form_for声明中,否则format.html响应块将是呈现而不是format.js。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM