Rails 3: Create button to run an action without page redirection?
I'm very new to Rails so here goes:
I know how to make a basic for that sends the results to another page with
<%= form_tag "Class/Action">
# and
<% submit_tag "Button name">
but what I want are a few buttons that run an action (and the page the user is on doesn't change or refresh).
Also, if you have a better alternative, here's exactly what I'm doing: A user is presented with choices and when he selects one, I want to push the choice to other clients via Juggernaut. Juggernaut is used throug开发者_如何转开发h Rails with the command Juggernaut.publish(channel, message), and so I need to call an action that does this command, without refreshing or changing his page.
Use a :remote => true
form and run the action via Ajax. This also gives you an easy opportunity to return either data, HTML, or JavaScript to let the user know what happened.
You just need to define the :remote => true in your form_tag. Here's an example...
<%= form_for(@folder, :remote => true) do |field| %>
<%= field.label :Name %>
<%= field.text_field :Name, :placeholder => "New Folder" %>
<%= field.submit "Create", :disable_with => "Saving..." %>
<% end %>
精彩评论