我应该使用AJAX来加载给它的下一个部分无需重新加载页面的链接。这里是链接:
I have a link that should use ajax to load a partial next to it without reloading the page. Here is the link:
<%= link_to 'test ajax link', profile_form_path , remote: true %>
下面是控制器的链接应该去的:
Here is the controller that the link should go to:
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
And here is the js view that the controller action goes to: profile_form.js.erb
$('.tab-edit-profile').html("<%= j render 'profile_form_p' %>");
然而,这是行不通的。浏览器返回此错误:
However, it does not work. The browser returns this error:
Template is missing
Missing template profiles/profile_form
下面是路由行文件:
get 'profile_form', to: 'profiles#profile_form'
如果我更改文件名profile_form.html.erb,页面加载,但没有使用AJAX明显。谁能告诉我,使用Ajax实现这个正确的方法是什么?谢谢你。
If I change the file name to profile_form.html.erb, the page loads but not using ajax obviously. Can anyone tell me the correct way to use ajax to implement this? Thanks.
你需要的是一个局部的,例如, _profile.html.erb
。下划线告诉Rails该文件呈现为局部的。在你的 profile_form.js.erb
你可以试试。
What you need is a partial, for example, _profile.html.erb
. The underscore tells Rails this file is rendered as partial. In your profile_form.js.erb
you can try.
$('.tab-edit-profile').html("<%= escape_javascript(render 'profile') %>");
这应该让你去用静态内容。对于动态,建立一个 @profile
实例变量和尝试。
That should get you going with static content. For dynamic, setup a @profile
instance variable and try.
$('.tab-edit-profile').html("<%= escape_javascript(render @profile) %>");
请务必使用渲染JSON:@profile在你的控制器响应