Rails link_to with remote:true不请求JS
试图渲染一个模态来更新任务,但是当使用remote:true时,它只是请求html格式,我甚至检查了不接受js的请求头。
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
我不确定是什么问题。 我已经尝试在link_to中说明格式并在控制器中有一个respond_to,但是它呈现原始文本html而不是在我的.js.haml中执行js
违规代码:
查看[.html.haml]:
= link_to 'Edit', edit_task_path(task), remote: true
控制器:
def edit @task = Task.find(params[:id]) render :show_form end
show_form.js.haml:
:plain $('#modal1').modal('open'); $('#modal1').html("#{escape_javascript(render partial: 'create_task', locals: {task: @task})});
此外,我还包含了所有必需的库,我甚至使用了remote:true其他在代码中提交表单的地方,一切正常。
在控制器中按照以下代码进行更改。
respond_to do |format| format.html { render layout: 'YOUR LAYOUT' } format.js end you can use template or text in place of layout.
在清单文件(application.js)
写下面的内容
//= require jquery //= require jquery_ujs