Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add additional params to a button_to form?

I want to have a Submit button. It updates one field on the submission; submission.state = :submitted

Now, I could make a custom route and a custom action and just post to that. But that seems really heavy-handed. Especially since I'll also have a reject button and possibly more. Needing a custom route & action for each of those seems downright silly to me.

It would be much nicer if I could do something like

button_to "Submit", submission_url(submission), :method => :put, :submission => { :state => :submitted }

Which would post to the submission's update method and update only the desired field.

But that doesn't work. How can I make it work? Or do you have a better idea of how to do this?

like image 429
chadoh Avatar asked Feb 03 '11 13:02

chadoh


2 Answers

The pull request mentioned by @AugustinRiedinger has been merged and is now available as of Rails 4.1.0. Now just add the params option:

params: { state: :submitted }
like image 67
jayelm Avatar answered Oct 16 '22 01:10

jayelm


It's not as concise, but without extending Rails, this will get me by:

= form_for submission, :html => { :class => "button_to" } do |f|
    = f.hidden_field :state, :value => :submitted
    = f.submit "Submit", :class => "link"
like image 33
chadoh Avatar answered Oct 16 '22 00:10

chadoh