Brian Roisentul
Brian Roisentul

Reputation: 4730

Link_to instead of button to submit a form

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

Upvotes: 0

Views: 2688

Answers (2)

Toby Hede
Toby Hede

Reputation: 37133

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.

Upvotes: 2

Gabriel Ščerbák
Gabriel Ščerbák

Reputation: 18570

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.

Upvotes: 2

Related Questions