简体   繁体   中英

Rails form with 3 submit buttons

I want to use a form to create an admin page... where there are checkboxes signifying which image the admin has selected, and then three submit buttons : approve, edit, delete.

Is form_tag the best way to do this? If not, then if I just use simple buttons, how can I connect them to gather data from the selected checkbox?

Thank you...

one way is you can create one form with three buttons. if you are using form_tag, you can create three buttons by doing submit_tag three times. for the submit_tags use the same :name parameter but different values. When handling the form submit check for which button they clicked and then reroute to appropriate action.

doing it this way avoids having to use js to share the checkboxes with three different forms and so on.

Having the same question, I've tried to use form_for and it seems to work. I just added a submit button for additional actions.

= form_for @request do |f|
  = render 'shared/error_messages', object: f.object

  = f.label :status
  = f.text_field :status

...some more fields  

  = f.submit "Save changes", class: "btn btn-large btn-primary" <--- This is initial button
  = f.submit "Accept", name: 'accept', class: "btn btn-large btn-primary"
  = f.submit "Reroute", name: 'reroute', class: "btn btn-large btn-primary"
  = f.submit "Solve", name: 'solve', class: "btn btn-large btn-primary"

Then in controller you check which button was pressed:

if !params[:accept].nil? 

if !params[:reroute].nil? 

if !params[:solve].nil? 

Name of the initial button you can find in the source of the rendered page. It's "commit" for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM