开发者

Link_to instead of button to submit a form

开发者 https://www.devze.com 2022-12-17 17:25 出处:网络
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

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 li开发者_高级运维nk_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


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消