#38
May 30, 2007

Multibutton Form

If you have a form with multiple buttons, you can detect which button was clicked by checking the passed parameters. Learn how in this episode.
Download (15.1 MB, 4:26)
alternative download for iPod & Apple TV (7.2 MB, 4:26)
<!-- projects/new.rhtml -->
<% if params[:preview_button] %>
  <%= textilize @project.description %>
<% end %>
...
<%= submit_tag 'Create' %>
<%= submit_tag 'Preview', :name => 'preview_button' %>
# projects_controller.rb
def create
  @project = Project.new(params[:project])
  if params[:preview_button] || !@project.save
    render :action => 'new'
  else
    flash[:notice] = "Successfully created project."
    redirect_to project_path(@project)
  end
end

RSS Feed for Episode Comments 14 comments

1. pimpmaster May 30, 2007 at 03:48

This is too funny. I was halfway through a post on railsforum asking how to do exactly this when my RSS alerted me to a new Railscast. Your timing could not be better!

d:)


2. vigosan May 30, 2007 at 06:04

I'm agree with the first comentary ;)


3. Geoff Buesing May 30, 2007 at 09:06

Clever stuff!


4. Alex G May 30, 2007 at 13:49

well done!


5. Zubin May 30, 2007 at 19:13

One way round the AJAX form problem is to have one submit button, and use a "preview" checkbox.


6. Gavin Sim May 31, 2007 at 08:57

Another great railscast! please keep them coming!


7. flyerhzm Feb 24, 2008 at 21:49

I found the multibutton form can be work only for form_for, but not for form_remote_for


8. Arnab Nov 21, 2008 at 18:53

Nice one. I wish this worked with image_submit_tag too.

The name attribute does not not get passed back in the params hash for image_submit_tag


9. Horace Ho May 15, 2009 at 23:40

How can I replace submit_tag buttons with link_to or link_to_function?

I want to use multiple link_to_function to replace the submit_tag, to prevent users from accidentally sending a form when press the enter key.

However, I cannot figure out a way to pass :name => 'preview_button' in link_to_function.

This is how I submit a form via link_to_function:

<%= link_to_function('Save', "$('ProductForm').submit()") %>

Please help
Thanks


10. Rilindo Foster Aug 10, 2009 at 18:25

For those who wanted to try this, keep in mind that this requires that Red Cloth be installed in order to utilize textilize:

http://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html#M001748

. To install it:

gem install RedCloth

Then add:

require "RedCloth"

To your environment.rb file.

For more information on setting it up, go here:

http://redcloth.org/


11. Venediktich Dec 02, 2009 at 13:23

Useful information! I recently ran into this problem, but also managed through this information


12. Milanka Dec 05, 2009 at 11:54

Excellent information! In time I have suffered with this issue, sorry that time did not read the article


13. Artemka Feb 04, 2010 at 05:22

I think i have same problem too, but i see there is no admin or support to reply our comment :)


14. Leksa Feb 15, 2010 at 23:51

it just sends me to a new web page rather than starting a download of a folder. Help??

Add your comment:

(SKIP THIS ONE)

(required)

(not shown)


(use pastie or gist for code)

sponsored by:
if you want to help:
required:
Get Quicktime Player
Give Back to Open Source