1

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.

4 Answers 4

2

You can give your submit tag a name.. ie

<%= form_for @thing do |f| %>
  <%= hidden_field_tag 'thing[is_accepted]' %>
  <%= f.submit "Accept", :name => 'accept' %>
  <%= f.submit "Reject", :name => 'reject' %>
<% end %>

Then you can detect the name in params[] and skip the '1'/'0' value.

1

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

<%= form_for @thing do |f| %>
  <%= f.hidden_field :accepted, :value => true %>
  <%= f.submit "Accept" %>
<% end %>

<%= form_for @thing do |f| %>
  <%= f.hidden_field :accepted, :value => false %>
  <%= f.submit "Reject" %>
<% end %>

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.

0

Depending on how you want your UI to work you might consider link_to_remote (part of the prototype helper) - you can specify an action, params etc, and have it return some JS that gets run.

1
  • Longer term, I suspect that's what I'll be doing, but I want to get things running non-ajaxy for now, and spruce it up later. Thanks for your suggestion! Jan 9, 2009 at 0:05
-1

If you're using map.resources in your routes.rb you should be able to do something like this:

map.resources :things, :member => {:accept => :get, :reject => :get}

Then in your controller:

def accept
  @thing = Thing.find(params[:id])
  @thing.is_accepted = true
  @thing.save
end

def reject
  @thing = Thing.find(params[:id])
  @thing.is_accepted = false
  @thing.save
end

And finally in your view:

<%= link_to 'Accept', accept_thing_url(@thing) %>
<%= link_to 'Reject', reject_thing_url(@thing) %>

Or if you are using Ajax:

<%= link_to_remote 'Accept', :url => accept_thing_url(@thing) %>
<%= link_to_remote 'Reject', :url => reject_thing_url(@thing) %>
1
  • I would suggest against adding new methods to your controller to address what is essentially a standard update. The logic to handle this is already built into your RESTful controller, so there really is no reason I can see to rewrite it.
    – PJ Davis
    Jan 8, 2009 at 22:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.