Multiple submit buttons with ajax
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 sev开发者_如何转开发eral 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?
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
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
精彩评论