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?
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'
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.
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?
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 Fauser's tutorial on
Ajax, available at O'Reilly, an excellent bargain.
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', !
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.
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.