Derek
Derek

Reputation: 9953

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 through 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.

Upvotes: 1

Views: 1167

Answers (2)

Altonymous
Altonymous

Reputation: 783

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 %>

Upvotes: 0

Dave Newton
Dave Newton

Reputation: 160321

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.

Upvotes: 4

Related Questions