views:

157

answers:

2

Hi,

I'm new at ruby on rails, and I wanted to accomplish the following:

I've got a list with check boxes(one per row) and I'd like to have a link button(not a common button or submit) so when I click it I call an action from a controller.

My questions are:

  1. How can I call a controller action with the link_to?
  2. How do I get the checkboxes values(which of them are checked) and fields' values so I can know to whom apply the action? I'm used to work with C#, and there the lists have an ItemDataBound method in where you can get easily every row lets say, but I can't figure anything similar here.

If it's not clear enough, I'm going to put an example:

Let's say I've got the following "screen":

Delete(link)

Article ID | Article Name

chkbox 1111 t-shirt

chkbox 2222 pants

chkbox 3333 boots

Now, let's say I'd like to delete the pants and boots. So I'll check their check boxes and then I'll press Delete. Now, I'd like to have at my Articles controller, at the method delete_article(for example) and then get the id and name for those checked articles, and delete them.

Thanks, Brian

+2  A: 

You can use link_to the standard way, check out the rails documentation on 'link_to'. The values from checkboxes can be get from the params hash. Just look out for the documentation.

Gabriel Ščerbák
Thank you. I read the documentation and one of its examples says the following:link_to "Profile", :controller => "profiles", :action => "show", :id => @profileIn my case, the :id paramater will be useless, so I didn't pass it(I don't know if I can do that), so I'm doing something like this:<%= link_to 'Do some action',:controller => 'announcements', :action => 'some_method' %>And it effectively goes to the Announcements controller, but to the Show action insted of the method I specified. Why?
Brian Roisentul
Have you specified the right method? You should try create.
Gabriel Ščerbák
+2  A: 

I would wrap the checkboxes in a form and then submit this form using the link (either with javascript or change the link to a form button and style as a link).

Rails assumes a RESTful approach out of the box, so a straight link will always hit a GET accessible action on your controller (generally index or show). GET actions should always be idempotent.

Toby Hede
Thank you. I'll do that. One final question: how do I change a button's style as a link?
Brian Roisentul
That's probably another question altogether :P. You can set the border, background-color and text color of input fields using CSS.
Toby Hede