Say there is a partial called _hello_world.html.erb
<% if toggle %>
HELLO WORLD ON
<%= link_to "Turn it off", what_should_come_here %>
<% else %>
HELLO WORLD OFF
<%= link_to "Turn it on", what_should_come_here %>
<% end %>
Intially this partial will be called from say an index file like this,
<%= render :partial => 'test/hello_world', :locals => {:toggle => true} %>
But after I click the toggle link it should switch between On and Off states which should basically render the own partial again overriding the previous partial. How to do this ?
Note: I have given the Question in http://sscce.org/ format.
instead of link_to
it would be better to use link_to_remote
index.html.erb
<div id="toggle_div">
<%= render :partial => 'test/hello_world', :locals => {:toggle => @toggle} %>
</div>
_hello_world.html.erb
<% if toggle %>
HELLO WORLD ON
<%= link_to_remote "Turn it off", my_action_path(:toggle => toggle) %>
<% else %>
HELLO WORLD OFF
<%= link_to_remote "Turn it on", my_action_path(:toggle => toggle) %>
<% end %>
in action
def myaction
value = params[:toggle]
// do something with toggle value
@toggle = value == "true" ? false: true
render :update do |page|
page.replace_html 'toggle_div', :partial => 'hello_world', :locals => {:toggle => @toggle}
end
end