如何通过rails3和jQuery中的ajax渲染部分内容

时间:2022-10-08 23:30:24

I could do sth like this in rails 2:

我可以在rails 2中这样做:

def show_rec_horses
  rec_horses = Horses.find(:all)
  page["allHorses"].replace_html :partial => "horses/recommended", :locals => 
 {:rec_horses => rec_horses}
end

How do I do this in Rails3 with jQuery. So how can I replace html of my div with a partial via ajax call(link_to :remote => true) in Rails3.

我如何使用jQuery在Rails3中执行此操作。那么如何在Rails3中用部分通过ajax调用(link_to:remote => true)替换我的div的html。

I tried sth like (in my show_rec_horses.js.erb):

我试过(在我的show_rec_horses.js.erb中):

$("#interactionContainer").update("<%= escape_javascript(render("horses/recommended"))%>");

And nothing happens. I tried some other variations but none worked. What am doing wrong? Should this be approached in some other way? Any answer would be greatly appreciated.

没有任何反应。我尝试了一些其他的变化但没有工作。怎么了?是否应该以其他方式处理?任何答案将不胜感激。

1 个解决方案

#1


25  

Okay, let's start with the controller.

好的,让我们从控制器开始吧。

def show_rec_horses
  @rec_horses = Horses.find(:all)

  respond_to do |format|
    format.html # show_rec_horses.html.erb
    format.js   # show_rec_horses.js.erb
  end
end

This will make sure the correct templates are being rendered for either a html request or a javascript request. If you just need to respond to a javascript request, delete the html part.

这将确保为html请求或javascript请求呈现正确的模板。如果您只需要回复javascript请求,请删除html部分。

Now let's say you have a page containing the following:

现在假设您有一个包含以下内容的页面:

<p><%= link_to 'Show Horses', show_rec_horses_path, remote: true %></p>

<div id="interactionContainer"></div>

Clicking the link will make a request to the show_rec_horses action in your controller, this action will render the javascript file show_rec_horses.js.erb.

单击该链接将向控制器中的show_rec_horses操作发出请求,此操作将呈现javascript文件show_rec_horses.js.erb。

This javascript file should contain something like this:

这个javascript文件应包含以下内容:

$('#interactionContainer').html('<%=j render partial: 'horses/reccommended', locals: { rec_horses: @rec_horses } %>')

Now your container will be filled with the content of horses/_reccommended.html.erb, which for example could contain:

现在你的容器将填充horse / _reccommended.html.erb的内容,例如可以包含:

<% rec_horses.all.each do |rec_horse| %>
  <p><%= rec_horse.name %></p>
<% end %>

#1


25  

Okay, let's start with the controller.

好的,让我们从控制器开始吧。

def show_rec_horses
  @rec_horses = Horses.find(:all)

  respond_to do |format|
    format.html # show_rec_horses.html.erb
    format.js   # show_rec_horses.js.erb
  end
end

This will make sure the correct templates are being rendered for either a html request or a javascript request. If you just need to respond to a javascript request, delete the html part.

这将确保为html请求或javascript请求呈现正确的模板。如果您只需要回复javascript请求,请删除html部分。

Now let's say you have a page containing the following:

现在假设您有一个包含以下内容的页面:

<p><%= link_to 'Show Horses', show_rec_horses_path, remote: true %></p>

<div id="interactionContainer"></div>

Clicking the link will make a request to the show_rec_horses action in your controller, this action will render the javascript file show_rec_horses.js.erb.

单击该链接将向控制器中的show_rec_horses操作发出请求,此操作将呈现javascript文件show_rec_horses.js.erb。

This javascript file should contain something like this:

这个javascript文件应包含以下内容:

$('#interactionContainer').html('<%=j render partial: 'horses/reccommended', locals: { rec_horses: @rec_horses } %>')

Now your container will be filled with the content of horses/_reccommended.html.erb, which for example could contain:

现在你的容器将填充horse / _reccommended.html.erb的内容,例如可以包含:

<% rec_horses.all.each do |rec_horse| %>
  <p><%= rec_horse.name %></p>
<% end %>