Rails remote form succeeds, but no data is returned












1















I have a simple remote form with one input and send button.
I created listener for ajax:success:



$ ->
$form = $('#post-form')

$form.on 'ajax:success', (e, data, status, xhr) ->
console.log e
console.log data
console.log status
console.log xhr


And my controller:



  def create
@post = Post.new(post_params)
@post.user_id = 3

if @post.save
render partial: 'posts/view', locals: { post: @post }, status: :ok
else
render json: @post.errors, status: :unprocessable_entity
end
end


I see that request passes on backend and status 200 is returned:



Rendered posts/_view.html.erb (3.0ms) [cache miss]
Completed 200 OK in 480ms (Views: 466.6ms | ActiveRecord: 7.0ms)


But in devtools I see:



enter image description here



What am I doing wrong?










share|improve this question

























  • I am also getting the same issue, did you find the solution?

    – Deepanshu Goyal
    Sep 4 '17 at 6:06






  • 1





    @DeepanshuGoyal Yeah, I stopped using rails.

    – Łukasz Szcześniak
    Sep 4 '17 at 12:36
















1















I have a simple remote form with one input and send button.
I created listener for ajax:success:



$ ->
$form = $('#post-form')

$form.on 'ajax:success', (e, data, status, xhr) ->
console.log e
console.log data
console.log status
console.log xhr


And my controller:



  def create
@post = Post.new(post_params)
@post.user_id = 3

if @post.save
render partial: 'posts/view', locals: { post: @post }, status: :ok
else
render json: @post.errors, status: :unprocessable_entity
end
end


I see that request passes on backend and status 200 is returned:



Rendered posts/_view.html.erb (3.0ms) [cache miss]
Completed 200 OK in 480ms (Views: 466.6ms | ActiveRecord: 7.0ms)


But in devtools I see:



enter image description here



What am I doing wrong?










share|improve this question

























  • I am also getting the same issue, did you find the solution?

    – Deepanshu Goyal
    Sep 4 '17 at 6:06






  • 1





    @DeepanshuGoyal Yeah, I stopped using rails.

    – Łukasz Szcześniak
    Sep 4 '17 at 12:36














1












1








1








I have a simple remote form with one input and send button.
I created listener for ajax:success:



$ ->
$form = $('#post-form')

$form.on 'ajax:success', (e, data, status, xhr) ->
console.log e
console.log data
console.log status
console.log xhr


And my controller:



  def create
@post = Post.new(post_params)
@post.user_id = 3

if @post.save
render partial: 'posts/view', locals: { post: @post }, status: :ok
else
render json: @post.errors, status: :unprocessable_entity
end
end


I see that request passes on backend and status 200 is returned:



Rendered posts/_view.html.erb (3.0ms) [cache miss]
Completed 200 OK in 480ms (Views: 466.6ms | ActiveRecord: 7.0ms)


But in devtools I see:



enter image description here



What am I doing wrong?










share|improve this question
















I have a simple remote form with one input and send button.
I created listener for ajax:success:



$ ->
$form = $('#post-form')

$form.on 'ajax:success', (e, data, status, xhr) ->
console.log e
console.log data
console.log status
console.log xhr


And my controller:



  def create
@post = Post.new(post_params)
@post.user_id = 3

if @post.save
render partial: 'posts/view', locals: { post: @post }, status: :ok
else
render json: @post.errors, status: :unprocessable_entity
end
end


I see that request passes on backend and status 200 is returned:



Rendered posts/_view.html.erb (3.0ms) [cache miss]
Completed 200 OK in 480ms (Views: 466.6ms | ActiveRecord: 7.0ms)


But in devtools I see:



enter image description here



What am I doing wrong?







ruby-on-rails ajax coffeescript ruby-on-rails-5






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited May 9 '17 at 17:35









Iceman

9,07162439




9,07162439










asked May 9 '17 at 17:32









Łukasz SzcześniakŁukasz Szcześniak

573516




573516













  • I am also getting the same issue, did you find the solution?

    – Deepanshu Goyal
    Sep 4 '17 at 6:06






  • 1





    @DeepanshuGoyal Yeah, I stopped using rails.

    – Łukasz Szcześniak
    Sep 4 '17 at 12:36



















  • I am also getting the same issue, did you find the solution?

    – Deepanshu Goyal
    Sep 4 '17 at 6:06






  • 1





    @DeepanshuGoyal Yeah, I stopped using rails.

    – Łukasz Szcześniak
    Sep 4 '17 at 12:36

















I am also getting the same issue, did you find the solution?

– Deepanshu Goyal
Sep 4 '17 at 6:06





I am also getting the same issue, did you find the solution?

– Deepanshu Goyal
Sep 4 '17 at 6:06




1




1





@DeepanshuGoyal Yeah, I stopped using rails.

– Łukasz Szcześniak
Sep 4 '17 at 12:36





@DeepanshuGoyal Yeah, I stopped using rails.

– Łukasz Szcześniak
Sep 4 '17 at 12:36












3 Answers
3






active

oldest

votes


















2














I'm not sure what you want to do with the data after you create the post, but in order to receive the data that was sent to an action via JSON, you have to return JSON from that action, as follows:



def create
@post = Post.new(post_params)
@post.user_id = 3

respond_to do |format|
if @post.save
format.html { render partial: 'posts/view', locals: { post: @post } }
format.json { render json: @post, status: :created }
else
format.html { render :new } # assuming you have a view file named "views/posts/new.html.erb"
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end


This way you can handle the request if it's JSON and you can handle the request if it's just plain HTML.






share|improve this answer
























  • This isn't answer to my question. I want to render html on backend and pass it to front irregardless of where request comes from.

    – Łukasz Szcześniak
    May 10 '17 at 12:12



















0














The technique I have used in the past is the replace a section of the DOM as part of the AJAX request, i.e.



def create
@post = Post.new(post_params)
@post.user_id = 3)
respond_to do |format|
format.js
end
end


And in /views/posts/create.js.erb



$("#new_item").html("<%= escape_javascript(render partial: 'posts/view', locals: { post: @post } ) %>"); 


Take a look at this link.






share|improve this answer
























  • I would rather avoid this approach. Is passing rendered html from backend impossible? I don't want js to know how view is rendered nor I want to remember that I delegated it.

    – Łukasz Szcześniak
    May 10 '17 at 12:14













  • To my knowledge this is the preferred and elegant way to do what you describe. This actually does pass rendered HTML from the server side, it just wraps it in a piece of Javascript that replaces the appropriate section of the page. You need to do this one way or another if you don't want to reload the page. Doing this on the front end using hand rolled JS would be messy, IMHO.

    – dps
    May 10 '17 at 12:20













  • If you don't want to use AJAX you can just do something like redirect_to @post to route back to the resource.

    – dps
    May 10 '17 at 12:23



















0














Quick TL;DR:



Assuming your above code, this is the json response you're looking for:



e.detail[0]


This vexed me a bit as well. Interrogating the one object that came back lead me to my answer- the result body is available in the event that comes back. My Rails app is not using jQuery, so the resultant event object is an ajax:success result.



The result has a field called detail which (in my case) contained three items: the data from the response body of the called request (which I believe is what you're looking for), the String "OK" and the XMLHttpRequest that made the call.



So, in your above form.on callback, if you interrogate e.details[0], I think you'll find the json data (also, the other params in the callback are unnecessary).



My assumption is that somewhere along the line, the callback logic for remote calls was simplified to a single parameter containing all the code. Looks like this happened when the removed the dependency to jQuery for UJS: https://guides.rubyonrails.org/working_with_javascript_in_rails.html#rails-ujs-event-handlers.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f43876352%2frails-remote-form-succeeds-but-no-data-is-returned%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    I'm not sure what you want to do with the data after you create the post, but in order to receive the data that was sent to an action via JSON, you have to return JSON from that action, as follows:



    def create
    @post = Post.new(post_params)
    @post.user_id = 3

    respond_to do |format|
    if @post.save
    format.html { render partial: 'posts/view', locals: { post: @post } }
    format.json { render json: @post, status: :created }
    else
    format.html { render :new } # assuming you have a view file named "views/posts/new.html.erb"
    format.json { render json: @post.errors, status: :unprocessable_entity }
    end
    end
    end


    This way you can handle the request if it's JSON and you can handle the request if it's just plain HTML.






    share|improve this answer
























    • This isn't answer to my question. I want to render html on backend and pass it to front irregardless of where request comes from.

      – Łukasz Szcześniak
      May 10 '17 at 12:12
















    2














    I'm not sure what you want to do with the data after you create the post, but in order to receive the data that was sent to an action via JSON, you have to return JSON from that action, as follows:



    def create
    @post = Post.new(post_params)
    @post.user_id = 3

    respond_to do |format|
    if @post.save
    format.html { render partial: 'posts/view', locals: { post: @post } }
    format.json { render json: @post, status: :created }
    else
    format.html { render :new } # assuming you have a view file named "views/posts/new.html.erb"
    format.json { render json: @post.errors, status: :unprocessable_entity }
    end
    end
    end


    This way you can handle the request if it's JSON and you can handle the request if it's just plain HTML.






    share|improve this answer
























    • This isn't answer to my question. I want to render html on backend and pass it to front irregardless of where request comes from.

      – Łukasz Szcześniak
      May 10 '17 at 12:12














    2












    2








    2







    I'm not sure what you want to do with the data after you create the post, but in order to receive the data that was sent to an action via JSON, you have to return JSON from that action, as follows:



    def create
    @post = Post.new(post_params)
    @post.user_id = 3

    respond_to do |format|
    if @post.save
    format.html { render partial: 'posts/view', locals: { post: @post } }
    format.json { render json: @post, status: :created }
    else
    format.html { render :new } # assuming you have a view file named "views/posts/new.html.erb"
    format.json { render json: @post.errors, status: :unprocessable_entity }
    end
    end
    end


    This way you can handle the request if it's JSON and you can handle the request if it's just plain HTML.






    share|improve this answer













    I'm not sure what you want to do with the data after you create the post, but in order to receive the data that was sent to an action via JSON, you have to return JSON from that action, as follows:



    def create
    @post = Post.new(post_params)
    @post.user_id = 3

    respond_to do |format|
    if @post.save
    format.html { render partial: 'posts/view', locals: { post: @post } }
    format.json { render json: @post, status: :created }
    else
    format.html { render :new } # assuming you have a view file named "views/posts/new.html.erb"
    format.json { render json: @post.errors, status: :unprocessable_entity }
    end
    end
    end


    This way you can handle the request if it's JSON and you can handle the request if it's just plain HTML.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered May 9 '17 at 21:17









    Greg AnswerGreg Answer

    601413




    601413













    • This isn't answer to my question. I want to render html on backend and pass it to front irregardless of where request comes from.

      – Łukasz Szcześniak
      May 10 '17 at 12:12



















    • This isn't answer to my question. I want to render html on backend and pass it to front irregardless of where request comes from.

      – Łukasz Szcześniak
      May 10 '17 at 12:12

















    This isn't answer to my question. I want to render html on backend and pass it to front irregardless of where request comes from.

    – Łukasz Szcześniak
    May 10 '17 at 12:12





    This isn't answer to my question. I want to render html on backend and pass it to front irregardless of where request comes from.

    – Łukasz Szcześniak
    May 10 '17 at 12:12













    0














    The technique I have used in the past is the replace a section of the DOM as part of the AJAX request, i.e.



    def create
    @post = Post.new(post_params)
    @post.user_id = 3)
    respond_to do |format|
    format.js
    end
    end


    And in /views/posts/create.js.erb



    $("#new_item").html("<%= escape_javascript(render partial: 'posts/view', locals: { post: @post } ) %>"); 


    Take a look at this link.






    share|improve this answer
























    • I would rather avoid this approach. Is passing rendered html from backend impossible? I don't want js to know how view is rendered nor I want to remember that I delegated it.

      – Łukasz Szcześniak
      May 10 '17 at 12:14













    • To my knowledge this is the preferred and elegant way to do what you describe. This actually does pass rendered HTML from the server side, it just wraps it in a piece of Javascript that replaces the appropriate section of the page. You need to do this one way or another if you don't want to reload the page. Doing this on the front end using hand rolled JS would be messy, IMHO.

      – dps
      May 10 '17 at 12:20













    • If you don't want to use AJAX you can just do something like redirect_to @post to route back to the resource.

      – dps
      May 10 '17 at 12:23
















    0














    The technique I have used in the past is the replace a section of the DOM as part of the AJAX request, i.e.



    def create
    @post = Post.new(post_params)
    @post.user_id = 3)
    respond_to do |format|
    format.js
    end
    end


    And in /views/posts/create.js.erb



    $("#new_item").html("<%= escape_javascript(render partial: 'posts/view', locals: { post: @post } ) %>"); 


    Take a look at this link.






    share|improve this answer
























    • I would rather avoid this approach. Is passing rendered html from backend impossible? I don't want js to know how view is rendered nor I want to remember that I delegated it.

      – Łukasz Szcześniak
      May 10 '17 at 12:14













    • To my knowledge this is the preferred and elegant way to do what you describe. This actually does pass rendered HTML from the server side, it just wraps it in a piece of Javascript that replaces the appropriate section of the page. You need to do this one way or another if you don't want to reload the page. Doing this on the front end using hand rolled JS would be messy, IMHO.

      – dps
      May 10 '17 at 12:20













    • If you don't want to use AJAX you can just do something like redirect_to @post to route back to the resource.

      – dps
      May 10 '17 at 12:23














    0












    0








    0







    The technique I have used in the past is the replace a section of the DOM as part of the AJAX request, i.e.



    def create
    @post = Post.new(post_params)
    @post.user_id = 3)
    respond_to do |format|
    format.js
    end
    end


    And in /views/posts/create.js.erb



    $("#new_item").html("<%= escape_javascript(render partial: 'posts/view', locals: { post: @post } ) %>"); 


    Take a look at this link.






    share|improve this answer













    The technique I have used in the past is the replace a section of the DOM as part of the AJAX request, i.e.



    def create
    @post = Post.new(post_params)
    @post.user_id = 3)
    respond_to do |format|
    format.js
    end
    end


    And in /views/posts/create.js.erb



    $("#new_item").html("<%= escape_javascript(render partial: 'posts/view', locals: { post: @post } ) %>"); 


    Take a look at this link.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered May 10 '17 at 11:17









    dpsdps

    485722




    485722













    • I would rather avoid this approach. Is passing rendered html from backend impossible? I don't want js to know how view is rendered nor I want to remember that I delegated it.

      – Łukasz Szcześniak
      May 10 '17 at 12:14













    • To my knowledge this is the preferred and elegant way to do what you describe. This actually does pass rendered HTML from the server side, it just wraps it in a piece of Javascript that replaces the appropriate section of the page. You need to do this one way or another if you don't want to reload the page. Doing this on the front end using hand rolled JS would be messy, IMHO.

      – dps
      May 10 '17 at 12:20













    • If you don't want to use AJAX you can just do something like redirect_to @post to route back to the resource.

      – dps
      May 10 '17 at 12:23



















    • I would rather avoid this approach. Is passing rendered html from backend impossible? I don't want js to know how view is rendered nor I want to remember that I delegated it.

      – Łukasz Szcześniak
      May 10 '17 at 12:14













    • To my knowledge this is the preferred and elegant way to do what you describe. This actually does pass rendered HTML from the server side, it just wraps it in a piece of Javascript that replaces the appropriate section of the page. You need to do this one way or another if you don't want to reload the page. Doing this on the front end using hand rolled JS would be messy, IMHO.

      – dps
      May 10 '17 at 12:20













    • If you don't want to use AJAX you can just do something like redirect_to @post to route back to the resource.

      – dps
      May 10 '17 at 12:23

















    I would rather avoid this approach. Is passing rendered html from backend impossible? I don't want js to know how view is rendered nor I want to remember that I delegated it.

    – Łukasz Szcześniak
    May 10 '17 at 12:14







    I would rather avoid this approach. Is passing rendered html from backend impossible? I don't want js to know how view is rendered nor I want to remember that I delegated it.

    – Łukasz Szcześniak
    May 10 '17 at 12:14















    To my knowledge this is the preferred and elegant way to do what you describe. This actually does pass rendered HTML from the server side, it just wraps it in a piece of Javascript that replaces the appropriate section of the page. You need to do this one way or another if you don't want to reload the page. Doing this on the front end using hand rolled JS would be messy, IMHO.

    – dps
    May 10 '17 at 12:20







    To my knowledge this is the preferred and elegant way to do what you describe. This actually does pass rendered HTML from the server side, it just wraps it in a piece of Javascript that replaces the appropriate section of the page. You need to do this one way or another if you don't want to reload the page. Doing this on the front end using hand rolled JS would be messy, IMHO.

    – dps
    May 10 '17 at 12:20















    If you don't want to use AJAX you can just do something like redirect_to @post to route back to the resource.

    – dps
    May 10 '17 at 12:23





    If you don't want to use AJAX you can just do something like redirect_to @post to route back to the resource.

    – dps
    May 10 '17 at 12:23











    0














    Quick TL;DR:



    Assuming your above code, this is the json response you're looking for:



    e.detail[0]


    This vexed me a bit as well. Interrogating the one object that came back lead me to my answer- the result body is available in the event that comes back. My Rails app is not using jQuery, so the resultant event object is an ajax:success result.



    The result has a field called detail which (in my case) contained three items: the data from the response body of the called request (which I believe is what you're looking for), the String "OK" and the XMLHttpRequest that made the call.



    So, in your above form.on callback, if you interrogate e.details[0], I think you'll find the json data (also, the other params in the callback are unnecessary).



    My assumption is that somewhere along the line, the callback logic for remote calls was simplified to a single parameter containing all the code. Looks like this happened when the removed the dependency to jQuery for UJS: https://guides.rubyonrails.org/working_with_javascript_in_rails.html#rails-ujs-event-handlers.






    share|improve this answer




























      0














      Quick TL;DR:



      Assuming your above code, this is the json response you're looking for:



      e.detail[0]


      This vexed me a bit as well. Interrogating the one object that came back lead me to my answer- the result body is available in the event that comes back. My Rails app is not using jQuery, so the resultant event object is an ajax:success result.



      The result has a field called detail which (in my case) contained three items: the data from the response body of the called request (which I believe is what you're looking for), the String "OK" and the XMLHttpRequest that made the call.



      So, in your above form.on callback, if you interrogate e.details[0], I think you'll find the json data (also, the other params in the callback are unnecessary).



      My assumption is that somewhere along the line, the callback logic for remote calls was simplified to a single parameter containing all the code. Looks like this happened when the removed the dependency to jQuery for UJS: https://guides.rubyonrails.org/working_with_javascript_in_rails.html#rails-ujs-event-handlers.






      share|improve this answer


























        0












        0








        0







        Quick TL;DR:



        Assuming your above code, this is the json response you're looking for:



        e.detail[0]


        This vexed me a bit as well. Interrogating the one object that came back lead me to my answer- the result body is available in the event that comes back. My Rails app is not using jQuery, so the resultant event object is an ajax:success result.



        The result has a field called detail which (in my case) contained three items: the data from the response body of the called request (which I believe is what you're looking for), the String "OK" and the XMLHttpRequest that made the call.



        So, in your above form.on callback, if you interrogate e.details[0], I think you'll find the json data (also, the other params in the callback are unnecessary).



        My assumption is that somewhere along the line, the callback logic for remote calls was simplified to a single parameter containing all the code. Looks like this happened when the removed the dependency to jQuery for UJS: https://guides.rubyonrails.org/working_with_javascript_in_rails.html#rails-ujs-event-handlers.






        share|improve this answer













        Quick TL;DR:



        Assuming your above code, this is the json response you're looking for:



        e.detail[0]


        This vexed me a bit as well. Interrogating the one object that came back lead me to my answer- the result body is available in the event that comes back. My Rails app is not using jQuery, so the resultant event object is an ajax:success result.



        The result has a field called detail which (in my case) contained three items: the data from the response body of the called request (which I believe is what you're looking for), the String "OK" and the XMLHttpRequest that made the call.



        So, in your above form.on callback, if you interrogate e.details[0], I think you'll find the json data (also, the other params in the callback are unnecessary).



        My assumption is that somewhere along the line, the callback logic for remote calls was simplified to a single parameter containing all the code. Looks like this happened when the removed the dependency to jQuery for UJS: https://guides.rubyonrails.org/working_with_javascript_in_rails.html#rails-ujs-event-handlers.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 25 '18 at 20:36









        abegosumabegosum

        601511




        601511






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f43876352%2frails-remote-form-succeeds-but-no-data-is-returned%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Costa Masnaga

            Fotorealismo

            Sidney Franklin