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?
Add params:{} at the end, it will generate hidden_field
So, as from this rails pull request : https://github.com/rails/rails/pull/10471
Here is what you can do to have your custom button_to.
In
application_helper.rb
, add these lines:And to use it:
Enjoy! Have fun Railing!
If I read things correctly what you are effectively wanting to do something specific when a standard rails form is submitted in the standard way.
Notice that when a form is submitted using e.g.
f.submit "Save Changes"
then
params[:commit] = "Save Changes"
The GOOD thing about this is that it can allow you to do some appropriate branching in the controllers update action.
The BAD thing is that it's brittle. If one day you or someone else decides to change the button text, things break.. which is bad.
K
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:I have something similar that works:
button_to "Submit", submission_url(submission, :submission => { :state => :submitted }), :method => :put
It's not as concise, but without extending Rails, this will get me by: