Let me preface this by noting that I'm new to Rails and hardly know
anything about Javascript...hence why I need help.
That said, I have a form which contains three text fields: site_id,
start_date, and end_date. I have a link_to_remote link which needs to
pass these three values to an action when I click it, but I can't for
the life of me figure out how to access / pass the three text field
values on so that I can use them.
you probably should make it a link_to_function instead which calls
form.submit() (where form is the id of your form). Then turn your form
into a remote_form_tag or remote_form_for. I think this would work
because you are triggering the onsubmit handler of the form which will
contain the AJAX request.
Not too familiar with the exact syntax for form_remote_tag, but it is
along those lines. Try gotapi.com/rubyrails for the full reference.
I've added the image there for a loading spinner but you could equally
write some text or whatever, just make sure there is an element with
id='spinner', or change the :before and :after calls (or remove them).
You can also use remote_form_for if you are changing a model directly.
I prefer this way. Finally, you can use restful or named routes in
place of the {:controller => ... :action => ...} hash
If you want to know more about selecting elements from the DOM using
prototype's $ helpers check that out on the prototype webiste http://www.prototypejs.org/
I actually got what I wanted using the link_to_remote. I'm not sure
the form_remote_tag was exactly what I wanted, but I appreciate the
help because I will certainly need that later on with what I'm doing.
The solution was this:
In application.js: (code stolen from another post in the group)
function serialize_fields(){
var fields = new Array();
fields.push(Form.Element.serialize('reservation_site_id'));
fields.push(Form.Element.serialize('reservation_start_date'));
fields.push(Form.Element.serialize('reservation_end_date'));
return fields.join('&');
}
In my controller (this is ugly as sin and there are probably a ton of
better ways to do this:
Anybody feel free to advise me where I could have done things better
because I'm still very very new to coding in general, and especially
new to Rails.
Oops, I mixed up information. Really, what you wanted to use was a
remote_form, though, as wildtangent suggested.
Use a form for multiple values that are entered on the page after the
page loads.
Use a link/button for any action using params that were already
available from the controller action.