我有一个表单部分,位于 ID 为“chapcomments”的 div 中,带有提交标签 -
<%= f.submit "Post", remote: true %>
在正确的 View 文件夹中,我有 create.js.erb -
$('#chapcomments').html("<%=j render 'shared/chap_comment_complete' %>");
在 Controller 中,我有格式 block ,其中包括
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 位于 View 中正确的 js.erb 文件中,但您需要将 remote: true
部分从提交标记移至 form_for
声明,否则将渲染 format.html 响应 block 而不是 format.js。
关于javascript - Rails - 将 ajax 添加到部分, 'undefined method ` 渲染'',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26164292/