# Form Submission

**URL:** <https://www.ruby-forum.com/t/form-submission/139421>\
**Category:** Rails\
**Created:** [June 3, 2008, 6:44pm UTC](https://www.ruby-forum.com/t/form-submission/139421 "2008-06-03T18:44:45Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [June 3, 2008, 6:44pm UTC](https://www.ruby-forum.com/t/form-submission/139421/1 "2008-06-03T18:44:45Z")

</div>

I have created a test for kids to take online. Right now I have all the  
navigation going through a submit button of a form. So, when a child  
hits the submit button it saves the response and redirects to the next  
question.

The question I have is the customer wants a top navigation as well where  
you can click on numbers representing the questions and have the answer  
saved and redirected to the question corresponding to the number they  
clicked. I have the navigation at the top but dont know how to submit  
the form from the link.

Is there a way to add an onclick event to the link that submits a form  
with a given id or something along those lines?

Thanks in advance,

Chris

---

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [June 3, 2008, 6:54pm UTC](https://www.ruby-forum.com/t/form-submission/139421/2 "2008-06-03T18:54:58Z")

</div>

Hi Chris,

Chris H. wrote:

> Is there a way to add an onclick event to the link  
> that submits a form with a given id or something  
> along those lines?

I think this will get you started:  
[http://www.ruby-forum.com/topic/115730](http://www.ruby-forum.com/topic/115730)

One hint with Google: I always preface my searches with ‘rails’ or ‘ruby  
on  
rails’. The link above was at the top of the list for ‘rails submit  
form  
from link’

Best regards,  
Bill

---

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [June 3, 2008, 9:31pm UTC](https://www.ruby-forum.com/t/form-submission/139421/3 "2008-06-03T21:31:24Z")

</div>

Hi Chris,

Chris H. wrote:

> When I change the link\_to\_remote to link\_to the  
> navigation works fine and the page redirects properly.  
> When I add the \_remote the page never refreshes to  
> a new question.

OK. Maybe I’m misunderstanding something here but if link\_to works  
fine,  
why don’t you use it?

> Any ideas what is going on here or is there any other  
> mway to submit a form with a link?

The reason link\_to\_remote isn’t working for you is that it creates an  
Ajax  
request. Your view is an html response and the browser ignores it. If,  
for  
whatever reason, you really want to use link\_to\_remove, you’re going to  
have  
to return a response the browser expects. I found Cody F.'s  
tutorial on  
Ajax, available at O’Reilly, an excellent bargain.

HTH,  
Bill

---

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [June 3, 2008, 9:43pm UTC](https://www.ruby-forum.com/t/form-submission/139421/4 "2008-06-03T21:43:04Z")

</div>

> OK. Maybe I’m misunderstanding something here but if link\_to works  
> fine,  
> why don’t you use it?

Sorry, maybe I am not explaining this properly or maybe I don’t know  
enough about this but the link\_to works for navigating from one question  
to the next. The problem is I need to submit the form when I move to  
another page. So, I tried to ask how to submit a form using a link.  
This way I could click the link, submit the form, and redirect to the  
next page.

I was told to use link\_to\_remote. I tried using this and it works to  
submit the form but the method I am calling is suppose to redirect to a  
new question after submitting the form using params[:question]. This is  
not working. So, if I could somehow combine the two to have the  
submitting of the form work and the redirect I would be all set.

I’m sorry if I am hard to understand. Thanks for trying to help.

Chris

---

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [June 3, 2008, 9:03pm UTC](https://www.ruby-forum.com/t/form-submission/139421/5 "2008-06-03T21:03:11Z")

</div>

Thanks Bill, I have been using google to look this up and I can’t get  
anything to work so I was hoping someone would throw out a new idea.  
When I use the link\_to\_remote everything works as far as passing the  
form data over and saving the responses to the database but none of the  
parameters seem to get passed over properly. When I check the db the  
results are saved but the page never redirects to a new question.

VIEW  
\<%= link\_to\_remote ‘Next&nbsp;Question’, :url =\> {:action =\> ‘next’,  
:question =\> @question.id}, :submit =\> “response” %\>

 \<%= render :partial =\> 't' + @question.section\_id.to\_s %\>

Controller  
def next  
question = Question.find(params[:question])  
…save response…  
redirect\_to(:action =\> ‘test’, :question =\> question.next\_question\_id)  
end

When I change the link\_to\_remote to link\_to the navigation works fine  
and the page redirects properly. When I add the \_remote the page never  
refreshes to a new question.

Any ideas what is going on here or is there any other way to submit a  
form with a link?

Thanks,

Chris

---

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [June 3, 2008, 10:16pm UTC](https://www.ruby-forum.com/t/form-submission/139421/6 "2008-06-03T22:16:57Z")

</div>

Hi Chris,

I have 2 suspicions and I will raise them, they might be the solution to  
your problem:

1)The submit: argument specifies the id that’s used as the parent of the  
form elements. Now your form I suppose is in the partial. Have you tried  
to  
use form\_remote\_for directly inside the partial instead of  
link\_to\_remote?  
link\_to works fine because it generates an href tag, link\_to\_remote is  
not  
comparable at all because it generate an hxr request. The redirect\_to in  
the  
controller will forward you to the next question.

2)are you sure you have the correct settings in your routes.rb for the  
:question variable to represent pages of an action?

To sum up, I think your problem would best be solved by removing the  
link\_to\_remote line and put a form\_remote\_for in your partial with the  
same  
arguments you supplied for the link\_to\_remote except for the :submit  
argument which you should remove.

Also, make sure in your controller to write redirect\_to(:action =\>  
‘next’,… instead of redirect\_to(:action =\> ‘test’, !

I hope it helps!

Cheers,

Amanda … wrote:

> to the next. The problem is I need to submit the form when I move to  
> I’m sorry if I am hard to understand. Thanks for trying to help.
> 
> ## Chris
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

–  
View this message in context:  
[http://www.nabble.com/Form-Submission-tp17628129p17632539.html](http://www.nabble.com/Form-Submission-tp17628129p17632539.html)  
Sent from the RubyOnRails Users mailing list archive at [Nabble.com](http://Nabble.com).

---

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [June 3, 2008, 10:28pm UTC](https://www.ruby-forum.com/t/form-submission/139421/7 "2008-06-03T22:28:30Z")

</div>

Thanks a lot for the feedback, I will look into the form\_remote\_for and  
give it a shot.

> Also, make sure in your controller to write redirect\_to(:action =\>  
> ‘next’,… instead of redirect\_to(:action =\> ‘test’, !

The action ‘next’ doesn’t have a view, it just saves the response and  
redirects to action ‘test’ with a new :question to view. There is a lot  
more code in the method I just tried to simplify it for questioning.

Thanks,

Chris

---

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [June 3, 2008, 11:09pm UTC](https://www.ruby-forum.com/t/form-submission/139421/8 "2008-06-03T23:09:58Z")

</div>

Ok, I am going to try to restate my question and hopefully it will make  
more sense.

I have several textareas on the screen (anywhere from 1 - 5). Right now  
I have them in a form and am using a submit button to pass the values to  
the controller. What I need to do is pass these values to the  
controller using links located outside of the actual form. When the  
link is click the values from the textareas are passed to the controller  
along with a :question parameter. The values of the textareas are saved  
into the db and the :question param is used in a query and then the page  
is redirected to another page with the result from the query as another  
param.

The part I cannot seem to find an answer for is how to pass the values  
of the textareas to the controller using link\_to().

So, I was trying to find out how to submit the form using a link and  
then just pass the :question param with it and have it all work. The  
link\_to\_remote wouldn’t allow me to redirect to another page based on  
the :question param.

I really hope that made more sense.

Thanks,

Chris
