Nikita Nikita - 2 months ago 9
Ajax Question

How to work with rails render with ajax javascript request

I have action in controller

def login
@message = params[:mess]
@user = 1
render :rend_log_form
end


the view - rend_log_form.js.erb

l = $("#log_form");
console.log(<%= @message %>);
l.html("<%= j(render(partial: 'login/log_form', locals: {user: @user})) %>");


and some javascript code

$.ajax({
type: "POST",
url: "/login",
data: {mess:"eee"},
success:function(data) {
console.log("SUCCESS POST");
console.log(data);
},
error:function(data) {
console.log("ERROR POST:");
console.log(data);
}
});


action and controller works ok if use link_to with remote: true, but with my javascript code I have in browser console:

ERROR POST:
Object


the Object.responseText - the code of rend_log_form.js.erb

How to render the form with javascript?

Answer

Thanks to Hary Bomrah, adding dataType:"html" to ajax call in js code has helped.