I'd like to save my Backbone model attributes on the Rails side. My code looks like:
var model = this.model.toJSON(); //this.model is my Backbone model
$.ajax({
url: "/users/" + this.model.get('id'),
type: "PUT",
dataType: "json",
data: model
});
For some reason, the attributes are not being saved on the Rails side. I think my problem is: I'm not completely sure what to pass in as the "data" in the AJAX request (JSON, the actual object, etc)?
Thanks for any suggestions
Related
Is it possible to use data with Rails.ajax in a Rails 5.1+ (without jQuery) app while making a GET request? Or data work only with POSTs?
I have the following
Rails.ajax
url: "/pages?title=lorem}"
type: 'GET'
And I'd rather having
Rails.ajax
url: 'pages'
type: 'GET'
data: { title: 'lorem' }
But when I try my params look like { "object Object"=>nil, "controller"=>"pages", "action"=>"index"}
Adding dataType: 'json' nor dataType: 'text' seem to change the params passed to the controller.
It's possible to use data with a GET request; however, Rails.ajax simply appends the data to the url.
if options.type is 'GET' and options.data
if options.url.indexOf('?') < 0
options.url += '?' + options.data
else
options.url += '&' + options.data
Other than readability, there doesn't look to be an advantage in using the data option. Your first technique of appending the data to the url yourself is doing the same thing as Rails.ajax under the hood.
Rails.ajax
url: "/pages?title=lorem"
type: 'GET'
However, this is similar to how Rails.ajax handles data for POST requests as well. It doesn't perform any transformation on the data. The data is passed through untouched to the XMLHttpRequest. Data is sent like this with options.data passed through as-is.
xhr.send(options.data)
I am trying to send my form data which includes some text fields and one image field to controller via ajax post request.
The form data looks like this:
And this is the ajax call i am making
$.ajax({
type: "POST",
dataType: "json",
processData: false,
data: {'new_post_data': data},
url: "/posts/newpostcreate",
success: function(resp, status){
}
});
The request is being sent but in the controller I am getting something like
ActionController::Parameters {"object Object"=>nil, "controller"=>"posts", "action"=>"new_post_create"} permitted: false>
I have spent many hours looking for a solution and trying different solutions but still it does not work. I'm not getting what I am missing.
Looks like you are using the wrong format and that your 'data' variable is empty (hint given in the Params: "object Object"=>nil). Try passing this:
$.ajax({
type: "POST",
dataType: "json",
data: { new_post_data: "test" },
url: "/posts/newpostcreate",
success: function(response){
console.log(response);
}
});
Edit: I realized you have a file attachment in your data. I think your safest bet is to create a FormData object and assign all your data to it.
https://developer.mozilla.org/en-US/docs/Web/API/FormData/FormData
I am not sure about processData: false. I never used it.
I create in my coffeescript code an object to pass via ajax post request which looks like this:
{"Category":[{"value1a":"v1a", "value2a":"v2a"}, {"value1b":"v1b", "value2b":"v2b"}]}
Via this code
$.ajax({
url: 'master/create',
type: "POST",
dataType: 'json',
contentType: 'application/json',
data: JSON.stringify(tickets)
})
tickets is the name of the object made.
but Rails is interpreting this like
Parameters:
{"Category"=>
[{"value1a"=>"v1a", "value2a"=>"v2a"}, {"value1b":"v1b", "value2b":"v2b"}],
"master"=>
{"Category"=>[{"value1a"=>"v1a", "value2a"=>"v2a"}, {"value1b":"v1b", "value2b":"v2b"}]
}
}
Where is this "master" coming from, and why is this happening?
If it helps, master is the name of the controller.
I have an action that calls a javascript file which contains an ajax method like this one:
$.ajax({
type: 'POST',
url: "<%= some_action(model) %>",
dataType: 'json',
data: { 'something': true },
success: function(received_data) {
// Do something with received_data
$('#notice').html("<%= escape_javascript(render 'layouts/flash_messages', flash: flash).html_safe %>");
}
});
The "some_action" tries to put some info into flash[:success], and I want to get it in the success function so that I can pass it to the render.
I have already tried the flash.now[:sucess], but nothing. It seems that it is only possible to do this if I write in the flash hash from the action that calls this javascript file - but I don't want this since "some_action" will generate dynamic content.
Is that something possible to to?
Thanks for the help!
you can send js request instead of json request .
and then in your "some_action.js.haml" file you can write
$('#notice').html("<%= escape_javascript(render 'layouts/flash_messages', flash: flash).html_safe %>");
what's happening here is that your javascript file is not getting refreshed hence content of html is not changing .
I have a newsletter box that submits via ajax like this:
function ajax_subscribe(){
$.ajax({
url: '/subscribe',
type: 'post',
data: ({
email: $('#email').val()
}),
success: function(msg){
$('#newsletter_form').hide();
$('#newsletter .done').removeClass('hide');
}
});
}
As you can see I'm currently using type: 'post'
Will that go to the create function in my controller automatically?
This is a great article on the default Rails routes: Rails Routing from the Outside In.
The routes will be the same regardless of whether you're using ajax or synchronous calls.