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 cl开发者_开发百科ick the link and have a div reload with different content generated by Ruby on 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.
If you're using jQuery, this might be easier with UJS than by hacking something together yourself. 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.
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):
$("#divtoreload").html("<%= @counter %>");
精彩评论