#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 4 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. ecco casual shoes Jun 30, 2010 at 23:12

great, I was halfway through a post on railsforum asking how to do exactly this


14. wariror Aug 19, 2010 at 13:42

thanks


15. wariror Aug 19, 2010 at 13:43

thanks


16. Nike Sb Dunks Aug 19, 2010 at 21:00

Great post! Thanks for share. I really appreciate what you post. If only i could think of a plugin to actually write. I will instantly grab your rss feed to stay informed of any updates.


17. replicahandbags Aug 20, 2010 at 00:30

I have always liked Outdoor movies, a child standing at the window, looked out from home to

the following. Will be able to see the staff busy figure, a huge white cloth has a child

hang up and soon will be able to see the movie.


18. snow boots Aug 31, 2010 at 01:40

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


19. levis belts Sep 01, 2010 at 21:10

I feel like I’m often looking for interesting things to read about a variety of subjects, but I manage to include your blog among my reads every day because you have interesting entries that I look forward to. Here’s hoping there’s a lot more great material coming!

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