# Using a link to submit a form

**URL:** <https://www.ruby-forum.com/t/using-a-link-to-submit-a-form/109848>\
**Category:** Rails\
**Created:** [July 19, 2007, 4:52pm UTC](https://www.ruby-forum.com/t/using-a-link-to-submit-a-form/109848 "2007-07-19T16:52:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![tdavis1198](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b9bd4f/32.png) [@tdavis1198](https://www.ruby-forum.com/u/tdavis1198)\
**Post date:** [July 19, 2007, 4:52pm UTC](https://www.ruby-forum.com/t/using-a-link-to-submit-a-form/109848/1 "2007-07-19T16:52:00Z")

</div>

I have a list of items. each item will have a checkbox next to it. I  
have a series of commands (in a sidebar div) that should perform an  
operation on the selected items in the list.

I know how to encapsulate that list with a form and then perform the  
operations on the checked items, but my question is, how do you (and  
can you) gather up the checked items on the click of the link and  
submit them to a controller for processing?

---

<div class="post-metadata">

**Author:** ![tdavis1198](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b9bd4f/32.png) [@tdavis1198](https://www.ruby-forum.com/u/tdavis1198)\
**Post date:** [July 19, 2007, 6:11pm UTC](https://www.ruby-forum.com/t/using-a-link-to-submit-a-form/109848/2 "2007-07-19T18:11:27Z")

</div>

This may not be much help, but you can pass multiple arguments by using  
the link helper (or any helper for that matter):

\<%= link\_to ‘Submit’, :action =\> ‘my\_action’, :id =\> @user.id, :name =\>  
@user.name %\>

But I don’t know how to do this using checkboxes or text fields that are  
not wrapped in form tags. You might try passing the value of the id that  
you gave to your checkboxes and then in your method that is called by  
the :action if they are checked or not.

---

<div class="post-metadata">

**Author:** ![tdavis1198](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b9bd4f/32.png) [@tdavis1198](https://www.ruby-forum.com/u/tdavis1198)\
**Post date:** [July 19, 2007, 6:27pm UTC](https://www.ruby-forum.com/t/using-a-link-to-submit-a-form/109848/3 "2007-07-19T18:27:06Z")

</div>

Shandy N. wrote:

> But I don’t know how to do this using checkboxes or text fields that are  
> not wrapped in form tags. You might try passing the value of the id that  
> you gave to your checkboxes and then in your method that is called by  
> the :action if they are checked or not.

This is a neat trick: (ajax-version)

link\_to\_remote(‘submit’, :url =\> {:action =\> ‘blabla’, :id =\>  
‘optional’}, :submit =\> “id\_of\_element\_around\_your\_checkboxes”)

The important part is the :submit option ofcourse, just wrap the  
elements you want to submit in a

 (or whatever) and everything in  
it will be posted as if it was in a form.  
Very usefull for posting rows of a table, because you can’t put a form  
around a complete row. Or for posting something that’s inside a form  
(forms in forms are not allowed)

Regards,

Stijn

---

<div class="post-metadata">

**Author:** ![tdavis1198](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b9bd4f/32.png) [@tdavis1198](https://www.ruby-forum.com/u/tdavis1198)\
**Post date:** [July 19, 2007, 8:47pm UTC](https://www.ruby-forum.com/t/using-a-link-to-submit-a-form/109848/4 "2007-07-19T20:47:38Z")

</div>

Sounds like a great solution. I will try it, thanks!

---

<div class="post-metadata">

**Author:** ![tdavis1198](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b9bd4f/32.png) [@tdavis1198](https://www.ruby-forum.com/u/tdavis1198)\
**Post date:** [July 19, 2007, 9:37pm UTC](https://www.ruby-forum.com/t/using-a-link-to-submit-a-form/109848/5 "2007-07-19T21:37:45Z")

</div>

On 7/19/07, Stijn P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> link\_to\_remote(‘submit’, :url =\> {:action =\> ‘blabla’, :id =\>  
> ‘optional’}, :submit =\> “id\_of\_element\_around\_your\_checkboxes”)
> 
> The important part is the :submit option ofcourse, just wrap the  
> elements you want to submit in a
> 
> (or whatever) and everything in  
> it will be posted as if it was in a form.  
> Very usefull for posting rows of a table, because you can’t put a form  
> around a complete row. Or for posting something that’s inside a form  
> (forms in forms are not allowed)

that is a great tip, Stijn! A shame I didn’t see your message  
yesterday, I  
really could’ve used it! Was trying to submit a form using  
link\_to\_remote  
and :with =\> Form.serialize(…) but I kept running into difficulty…  
This  
works perfectly, thanks!

Adam

---

<div class="post-metadata">

**Author:** ![tdavis1198](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b9bd4f/32.png) [@tdavis1198](https://www.ruby-forum.com/u/tdavis1198)\
**Post date:** [July 20, 2007, 12:09am UTC](https://www.ruby-forum.com/t/using-a-link-to-submit-a-form/109848/6 "2007-07-20T00:09:29Z")

</div>

Stijn P. wrote:

> Shandy N. wrote:
> 
> > But I don’t know how to do this using checkboxes or text fields that are  
> > not wrapped in form tags. You might try passing the value of the id that  
> > you gave to your checkboxes and then in your method that is called by  
> > the :action if they are checked or not.
> 
> This is a neat trick: (ajax-version)
> 
> link\_to\_remote(‘submit’, :url =\> {:action =\> ‘blabla’, :id =\>  
> ‘optional’}, :submit =\> “id\_of\_element\_around\_your\_checkboxes”)
> 
> The important part is the :submit option ofcourse, just wrap the  
> elements you want to submit in a
> 
> (or whatever) and everything in  
> it will be posted as if it was in a form.  
> Very usefull for posting rows of a table, because you can’t put a form  
> around a complete row. Or for posting something that’s inside a form  
> (forms in forms are not allowed)
> 
> Regards,
> 
> Stijn
> 
> Hey, where were you about 2 1/2 weeks ago? I got to admit, this sounds  
> like a really good idea. I might have to go back and change things in my  
> forms. Thanks Stijn
> 
> ~S
