In Rails, how do you render JSON using a view ?

In Rails, how do you render JSON using a view ?

Add Comment


  • 2 Answer(s)

    Using respond_to block render the template in app/views/users/_show.json.erb. : 

    respond_to do |format|
         format.json
         render :partial => "users/show.json"
    end
    

    Answered on January 11, 2019.
    Add Comment

    They view users/show.json.erb should be rendered when make a request for the JSON format,

    {
         "first_name": "<%= @user.first_name.to_json %>",
         "last_name": "<%= @user.last_name.to_json %>"
    }
    

    Answered on January 11, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.