I have a scaffold for a datatable in Rails 3.2. When I’m viewing the form I have a combobox which the user can change and the value will be instantly stored in the db without him having to go into edit mode and change it there and press save.
I have captured the changed value using coffeescript:
$(document).ready ->
$("#change_department").change ->
$ajax
url: "/Locations/" + # need to set id and new value here
success: (data) ->
alert data # will need to write a confirmation below the combobox
What I’m scrathing my head about is what controller action I should call and how I should prepare the data. I’m thinking that creating a new controller action where I would pass in the id of the model and the new value in the combobox and in the controller I would find the model and pass in the changed value. This should probably be a get method with two params in the url (I’m not sure how to do this using post). Is this a good aproach or would anybody suggest alternative method?
How would I get the id and the changed value in the coffeescript code above?
Thanks
1) When updating data, use a PUT request. That’s the basic RESTful style of Rails (
GET→show,POST→create,PUT→update,DELETE→ destroy). So basically, the action to call isLocationsController#update(judging by the code you posted). No need for a new action2) Put the entire update path in a
data-attribute on the element. I’m assuming you mean “dropdown” when you say “combobox”, in which case it’d be something like<select id="change_department" name="location[department]" data-uri="/Locations/23"> ...On the controller-side the update action can remain unchanged.