javascript 在 Rails 3 中渲染部分的 AJAX 调用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17246411/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AJAX call to render partial in Rails 3
提问by Hyman
I have a user's dashboard page where the left side has a sidebar with links like Projects, Blogs, etc. I want to be able to click a link such as the "Projects" link and then in the main area, the view for projects would load up (view would be a bullet list of projects) without having to refresh the page. I'm trying to make this happen with AJAX, but things are not working.
我有一个用户的仪表板页面,其中左侧有一个侧边栏,其中包含项目、博客等链接。我希望能够单击“项目”链接等链接,然后在主区域中,项目视图将加载(视图将是项目的项目符号列表)而无需刷新页面。我正在尝试使用 AJAX 实现这一点,但事情并没有奏效。
Here's how I think it's supposed to work. The user clicks on the sidebar link in show.html.erb
这就是我认为它应该工作的方式。用户点击侧边栏链接show.html.erb
<%= link_to 'Projects', '/dash', remote: true %>
where /dash
is configured to route to users#show in the config/routes.rb
file like this:
where/dash
配置为路由到config/routes.rb
文件中的 users#show,如下所示:
match '/dash' => 'users#show'
And then, the show
action is called inusers_controller.rb
controller:
然后,show
在users_controller.rb
控制器中调用操作:
def show
@user = User.find(params[:id])
@projects = @user.projects
respond_to do |format|
format.html # renders show.html.erb
format.js # renders show.js.erb
end
end
Where show.js.erb
is executed. My show.js.erb
file has this one line:
在哪里show.js.erb
执行。我的show.js.erb
文件有这一行:
$('#ajax').html("<%= escape_javascript(render(:partial => 'projects/index')).html_safe %>");
And that is supposed to modify the #ajax div in my show.html.erb
:
这应该修改我的#ajax div show.html.erb
:
<div id="ajax">
<%= render :template => 'projects/index' %>
</div>
The app/views/projects/index.html.rb
takes in @projects and gives a list, like this:
将app/views/projects/index.html.rb
在@projects需要并给出了一个列表,就像这样:
<% @projects.each do |project| %>
<p><%= project.name %></p>
<% end %>
Obviously, I'm doing things wrong because this is not working. What am I doing wrong? Is there some code I need to change elsewhere? I'm on Rails 3.2.13, so the lines //=jquery
and //=jquery_ujs
under app/assets/javascripts/application.js
should have imported the necessary jQuery and AJAX functionality.
显然,我做错了,因为这是行不通的。我究竟做错了什么?我需要在其他地方更改一些代码吗?我在 Rails 3.2.13 上,所以行//=jquery
和//=jquery_ujs
下面app/assets/javascripts/application.js
应该导入了必要的 jQuery 和 AJAX 功能。
回答by Ashitaka
The problem is that in your show.js.erb
file you are trying to render a partial but you are passing it a file that is not a partial. Partial files begin with an underscore _
.
问题在于,在您的show.js.erb
文件中,您试图呈现部分文件,但您传递的文件不是部分文件。部分文件以下划线开头_
。
So, to avoid duplicating code, here's what I'd do:
因此,为了避免重复代码,我会这样做:
# app/views/projects/show.js.erb
$('#ajax').html("<%= escape_javascript(render :partial => 'index', :locals => { :projects => @projects } ) %>");
# app/views/projects/index.html.erb
<%= render :partial => 'index', :locals => { :projects => @projects } %>
# app/views/projects/**_**index.html.erb
# Include here the code that you previously had in index.html.erb
# Don't forget to change @projects to projects
<% projects.each do |project| %>
<p><%= project.name %></p>
<% end %>
Also, you shouldn't use match
in your routes, as you pretty much will never need your routes to be accessible by both GET and POST methods. So try to use get
instead of match
.
此外,您不应该match
在您的路线中使用,因为您几乎永远不需要通过 GET 和 POST 方法访问您的路线。所以尝试使用get
而不是match
.