I am retrofitting some code to a Rails 2.3.8 application. The whole app uses Prototype, but I need to add some functionality to one controller and its views, so I am switching the old prototype code to jQuery and using a different layout.
So I can’t use link_to_remote in these views any more. I have a situation where I need to “clear out” an attribute for the record in question, in an “edit” action.
I have created a hyperlink with a specific id and bound the click function to it in application.js. I have appended the name of the attribute to the end of this specific id.
So I have two things going into this “remove_attribute” action. I need the id of the record, and the name of the attribute.
I am just serializing the form and submitting. But I need to the add the id of the hyperlinked I clicked!
In my application.js, I have:
hash = { type: "POST", url: "/vehicle_applications/remove", data: $("#myform").serialize() };
$.ajax(hash);
return false;
But you can see that I don’t have the id of the hyperlink in there. I know that to get the id of the hyperlink I refer to:
$(this).attr('id')
But my question is, how do I get a variable name and that value appended on to the results of $(“#myform”).serialize()?
Also, how screwed up is my approach?
jQuery’s
serializeproduces a query string:So you just need to append another component:
Depending on your routes, you might want to
idin the URL:This version should be more typical for Rails and RESTful routing but I don’t know what your routes look like.