我有一个链接,应该使用 ajax 加载它旁边的部分内容,而无需重新加载页面。链接在这里:
<%= link_to 'test ajax link', profile_form_path , remote: true %>
这是链接应该转到的控制器:
class ProfilesController < ApplicationController
def profile_form #the action is currently static so there is nothing in the controller
end
end
这是控制器操作转到的 js 视图:
profile_form.js.erb
$('.tab-edit-profile').html("<%= j render 'profile_form_p' %>");
然而,它不起作用。浏览器返回此错误:
Template is missing
Missing template profiles/profile_form
这是路由文件中的行:
get 'profile_form', to: 'profiles#profile_form'
如果我将文件名更改为 profile_form.html.erb,页面会加载,但显然不使用 ajax。谁能告诉我使用ajax来实现这个的正确方法?谢谢。
从您上面发布的内容来看,这应该可以正常工作。我唯一能想到的是你没有包含所需的 javascript 库和link_to
helper 实际上并没有发出 AJAX 请求。
您表示您正在使用 Rails 4,因此我假设您也在使用 Asset Pipeline。请确保您的文件中有以下几行app/assets/javascripts/application.js
file
//= require jquery
//= require jquery_ujs
并且 javascript 已包含在所使用的布局中ProfilesController
。假设布局是application.html.erb
,确保您有
<%= javascript_include_tag "application" %>
in the <head>
布局的一部分。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)