简体   繁体   English

Rails / AJAX部分渲染

[英]Rails/AJAX partial rendering

I made an application that has a project model. 我创建了一个具有project模型的应用程序。 The model has some information stored in it, and a user can add comments to the project (using the comment model). 模型中存储了一些信息,用户可以向project添加注释(使用comment模型)。 In the show view of a project I want the user to be able to switch between an "info" partial (containing the project information, and a "comment" partial (containing the comments wrote on the project). I want to do this using AJAX. So I will have two buttons: Information & Comments. 在项目的show视图中,我希望用户能够在“info”部分(包含项目信息和“注释”部分(包含项目中写的注释))之间切换。我想使用AJAX。所以我将有两个按钮:信息和评论。

Now I know how to render a partial based on a "remote link", but I'll also have to find out which link was clicked. 现在我知道如何基于“远程链接”呈现部分,但我还必须找出点击了哪个链接。 So far I can render one partial when one link is clicked like so: 到目前为止,我可以在点击一个链接时渲染一个部分,如下所示:

// In show.html.haml

= link_to("Information", :project, :id => "link_one", :remote => true)
= link_to("Comments", :project, :id => "link_two", :remote => true)

#partial_window


// In show.js.haml
$("#partial_window").html("#{j(render("comments"))}")

Now this renders the _comment.html.haml partial when I click on one of the links. 现在,当我点击其中一个链接时,这将呈现_comment.html.haml部分。 What I need to know is how to check which link was clicked, and then render the appropriate partial: _info.html.haml or _comments.html.haml . 我需要知道的是如何检查单击了哪个链接,然后呈现相应的部分: _info.html.haml_comments.html.haml

Thanks in advance for your help! 在此先感谢您的帮助!

Something like this should work. 这样的事情应该有效。 We are going to use nested routes. 我们将使用嵌套路由。 Check out ryan's screencast (a little old, but it gets the point across) or this more updated version about nested forms (uses the same principles). 查看ryan的截屏视频有点旧 ,但它得到了重点)或关于嵌套表单的更新版本 (使用相同的原理)。 You'll have to pay for the updated version, but I find my RailsCast subscription to be more than worth the $9/month. 您将需要支付更新版本,但我发现我的RailsCast订阅价值超过9美元/月。 Also, here are the docs for examples. 另外,这里是示例文档

config/routes.rb

resources :projects do
  resources :comments
end

comments_controller.rb

class CommentsController < ApplicationController
  def index
    project = Project.find params[:project_id]
    @comments = project.comments
    respond_to do |format|
     format.html #responds with default html file
     format.js #this will be the javascript file we respond with
    end
  end
end

views/comments/index.js.erb

$('#holderDiv').empty().append('<ul> <%= j render @comments %> </li>')

This uses a nifty thing of rails that looks for a comment partial and renders it for each comment in @comments . 这使用了一个很棒的rails来查找comment部分并为@comments每个注释呈现它。 The j helper escapes javascript and pretty much inserts the rendered partial into the append function. j帮助器转义javascript并且几乎将渲染的部分插入到append函数中。

views/comments/_comment.html.erb

 <li> <%= @comment.description %> </li>

So we've now cleared the #holdingDiv and inserted our comments. 所以我们现在已经清除了#holdingDiv并插入了我们的评论。 For information , maybe something like this: 有关information ,可能是这样的:

projects_controller.rb

class ProjectsController < ApplicationController
  def index
    @project = Project.find params[:id]
    respond_to do |format|
      format.html
      format.js
    end
  end
end

views/project/index.js.erb

 $('#holderDiv').empty().append('<%= j render 'information', information: @project.information %>')

views/project/_information.html.erb

<h2>
  <%= information.title %>
</h2>
<p>
  <%= infomration.details %>
</p>

Then, your remote links would be something like: 然后,您的远程链接将是这样的:

= link_to("Information", @project, :remote => true)
= link_to("Comments", project_comments_url(@project), :remote => true)

I had to make some assumptions about what your data structures were. 我不得不对你的数据结构做出一些假设。 Let me know where I've confused you. 让我知道我困惑你的地方。

Also, I am sure I have some typos, sorry for that. 此外,我确信我有一些错别字,抱歉。 I did not test this, just went off the top of my head. 我没有测试过这个,只是脱了我的头顶。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM