One of the things I’m doing includes several links on the show view. For instance, I have a link (or button) for ‘Accepting’, and another one for ‘Rejecting’. Click on Accept, and the model updates the is_accepted field as true, click on Reject, and the is_accepted field is false.
Now, how best do I handle this? In ASP.NET, I would have simply created a LinkButton and written a handler, but Rails doesn’t work that way, so I’m trying to figure out how to essentially replicate what a LinkButton would do.
Right now, I’m coding two forms on the same view, nearly identical, that look like this:
<%= form_for @thing do |f| %> <%= hidden_field_tag 'thing[is_accepted]', '1' %> <%= f.submit 'Accept' %> <% end %> <%= form_for @thing do |f| %> <%= hidden_field_tag 'thing[is_accepted]', '0' %> <%= f.submit 'Reject' %> <% end %>
This feels weird to me, but I can’t seem to find anything that says this is the wrong way to do it.
I could, I assume, dry things up by using a partial and/or a helper method, but I wanted to make sure I’m on the right track and not doing something totally wrongly.
I think you’re going about it the right way. One way to clean up your forms is by using the model form helpers all the way through, so you’d end up with something like
But other than that, it looks like the right way to go about it. I would suggest against creating new methods to do this, because you’re not doing anything outside of normal web requests (updating a model in this instance).
Using the submit tag as the switch and detecting it in params[] is also a good way, but I usually prefer to keep my controllers as vanilla as possible. In the end, both of these ways would end up with the same amount of ‘stuff’ in the UI, so whichever style you’d rather use should be fine.