简体   繁体   English

Rails 3.1:使用 Link_to 更新 Div

[英]Rails 3.1: Update Div with Link_to

Here's what I have:这是我所拥有的:

<%= link_to "Reload Div", {:controller => "my_controller", :action => "my_action"}, remote => true %>

I want to be able to click the link and have a div reload with different content generated by Ruby on Rails.我希望能够单击链接并重新加载 div,其中包含 Ruby 在 Rails 上生成的不同内容。 How can I do this?我怎样才能做到这一点?

For simplicity... I simply want to be able to click the link and reload the div with a number one higher than the previous.为简单起见...我只是希望能够单击链接并重新加载比前一个高一号的 div。

If you're using jQuery, this might be easier with UJS than by hacking something together yourself.如果您使用的是 jQuery,那么使用 UJS 可能比自己破解一些东西更容易。 Maybe something that does this:也许这样做:

$('a.reload').click(function() { $(this.parentNode).load(this.href); return false; });

If rails is incrementing the number, I'm not sure why you wouldn't just use link_to as you have specified in your question as you have everything set up correctly with your controller, action, and the remote attribute.如果 rails 正在增加数字,我不确定您为什么不使用您在问题中指定的 link_to,因为您已经使用 controller、操作和远程属性正确设置了所有内容。

Here is your action:这是您的操作:

def my_action
  @counter = 1 #Insert whatever logic you need to increment the number.
end

Here is your view (my_action.js.erb):这是您的观点 (my_action.js.erb):

$("#divtoreload").html("<%= @counter %>");

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

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