views:

287

answers:

2

First of all, I'm new at Ruby on Rails, so if there's a better practice than what I'm doing, please let me know.

What I'd like to do is to have multiple ajax submit buttons to perform different actions for a list of items with check boxes. So, when I select as many check boxes as I want, then I can choose what to do with them.

I could partially solve this using ajax as follows:

    remote_form_for :profile, :url => {:controller => 'announcements', :action => 'deactivate'}, :html => { :method => :put} do |f| 
      f.submit 'Publish', :confirm => 'Are you sure?'
#Then the list

This works great. What I'd like to do now is to add another "submit" or button, so I can perform several actions, so I guess the :url statement at the remote_form_for will be replaced for something like that per each button. For example:

Publish button: perform some action in some controller. Deactivate button: perform another action. Mark as Read: perform another action.

Is it clear?

+2  A: 

add as many "submits" as you like. All submits have the 'name' in the input tag set to 'commit' and the value set to the label. So you can check by looking in the params submitted:

 remote_form_for :profile, :url => {:controller => 'announcements', :action => 'deactivate'}, :html => { :method => :put} do |f| 
      f.submit 'Publish', :confirm => 'Are you sure?'
      f.submit 'Something Else"
 # Then the list

in the controller

def deactivate

   case params[:commit]
     when 'Publish' then do something
     when 'Something Else' then do something else

   end

end
glongman
Thank you. I've read that this does not work for IE as it's written. I guess that something else is needed, like a :name attribute for the submit button or something else. Do you know what is that?
Brian Roisentul
you can override the name easily. f.submit "Something Else", :name => 'somethingelse'. Then just check for params[:somethingelse]
glongman
I can't get this to work. When I click on the "Something Else" submit button, in the params I get "Publish" one instead...why?
Brian Roisentul
Uhm...I've been reading some articles and it seems that on Ajax forms multiple submit buttons won't be recognized, and they will only recognize the first one, as it's happening to me. What should I do then?
Brian Roisentul
I've seen this workaround: http://blog.railsformers.com/article/ajax-forms-with-multiple-submit-buttons-bug but it's really ugly. It will disable the other buttons just to avoid them to submit, and it won't enable again. There's another solution using a hidden field but I'm not sure how to implement that.
Brian Roisentul
A: 

Finally, I could solve it by myself.

It's well known there's a bug for multiple submit buttons on ajax forms. I used the hidden workaround as explained here.

Thanks, Brian

Brian Roisentul