【发布时间】:2014-01-25 18:14:08
【问题描述】:
我有一个链接应该使用 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 来实现这个的正确方法?谢谢。
【问题讨论】:
-
你能发布你的路线吗? HTTP 动词可能没有对齐。
-
当然,刚刚发布。谢谢。
-
你收到一个 HTML 格式的请求,并且你有一个 .js.erb 文件(ERB 文件最后生成一个 JS 文件),这就是 Rails 抱怨的原因:响应 HTML 格式,他正在寻找
profile_form.html.erb。您可以创建此文件并自己插入脚本标签:<script type="text/javascript">$('.tab-edit-profile').html("<%= j render 'profile_form_p' %>");</script> -
@MrYoshiji ,谢谢,但这不起作用,因为那样视图将不会通过 ajax 加载,整个页面将转到链接。
-
你使用的是什么版本的 Rails?
标签: javascript jquery ruby-on-rails ajax ruby-on-rails-4