While on a html page, I’m using an asynchronous XMLHttpRequest request in my Javascript to update a value in my database for a Ruby on Rails application. This update needs to happen “behind the scenes” without the user’s knowledge. Unfortunately, because there is no output html to this request (it is an asynchronous request), it’s very hard to debug why the database value is not updating.
In the initial html.erb file, the javascript is as follows:
function NAME() {
var url = '/custom_path'; // this path points to the update method in my controller
var request = new XMLHttpRequest();
var params = null;
request.open("POST", url, true); // open asynchronous post request
request.send(params);
}
I don’t actually pass any parameters because the only value that I need is stored in a cookie (you’ll see in the excerpt my code for the method). My Routes file includes the following line:
match'/customer_path', :to => 'activities#update'
Finally, the code to my “update” method in the Activity table is as follows:
def update
@dummy_var = Activity.find(cookies[:activity_id])
@dummy_var.column_var = false
render :layout => false
end
I’m trying to update the value of “column_var” in my Activity table for the row with the :id column specified by the cookie. Any ideas where I’m going wrong? I have confirmed that the Javascript function is being called and that cookies[:activity_id] has been set properly.
Thanks in advance!
I think the answer is pretty simple here, try calling save on the @dummy_var instance variable.
To test this sort of thing out with ajax, you can use Rails.logger.debug, which accepts a string. You can then check the logs with debug outputs, see what happens. Also you can use something like firebug to see if the request responds with an error.