Search code examples
ruby-on-rails-4

What does this line of code means in ruby?


Someone please give me a rough meaning of this code

render json: {:status => 1}

Solution

  • The render function in rails does the heavy lifting of rendering your application content for use by a browser or client. You can text, JSON, XML, html.erb and others. Now adding json after render will send everything in json format. Like following example will send user information in your response

    @user = User.first
    render json: @user
    

    Same we can send text

    render html: "<h1>Hi this is html</h1>"
    

    or we want to send xml

    render xml: @user
    # or
    render xml: {:status => 1}
    

    Now second part is actual hash where you define status as a key and value is 1 you can create hash like

    hash = Hash.new(status: 1)
    render json: hash
    

    This will send all user information in json format. but your code is making content in directly inline

    render json: {:status => 1}
    # or
    render json: {status: 1}
    # or
    render json: {name: 'my name', age: 23, status: 1}
    

    But this is data being sent if we want to send status code we can use

    render json: {:status => 1},