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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 07:37:27  来源:igfitidea点击:

AJAX call to render partial in Rails 3

javascriptruby-on-railsajaxruby-on-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 /dashis configured to route to users#show in the config/routes.rbfile like this:

where/dash配置为路由到config/routes.rb文件中的 users#show,如下所示:

match '/dash' => 'users#show'

And then, the showaction is called inusers_controller.rbcontroller:

然后,showusers_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.erbis executed. My show.js.erbfile 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.rbtakes 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 //=jqueryand //=jquery_ujsunder app/assets/javascripts/application.jsshould 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.erbfile 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 matchin your routes, as you pretty much will never need your routes to be accessible by both GET and POST methods. So try to use getinstead of match.

此外,您不应该match在您的路线中使用,因为您几乎永远不需要通过 GET 和 POST 方法访问您的路线。所以尝试使用get而不是match.