How to send simple json response in Rails?

前端 未结 4 1940
一向
一向 2020-12-03 00:46

I need to send json response depends on user entered data in input, but I\'m failing to send simple json request.

I followed this article - http://paydrotalks.com/po

相关标签:
4条回答
  • 2020-12-03 01:22

    One Simple way is:

    def my_action 
    
     render :json => {:name => "any name"}
    
    end
    
    0 讨论(0)
  • 2020-12-03 01:40

    I usually use this method to return json data:

    msg = {:token => token, :courseId => courseId}
    render :json => msg
    
    0 讨论(0)
  • 2020-12-03 01:42

    Not sure about dealing with custom MIME, but as for rendering JSON, this should work:

    def testme
      respond_to do |format|
        msg = { :status => "ok", :message => "Success!", :html => "<b>...</b>" }
        format.json  { render :json => msg } # don't do msg.to_json
      end
    end
    

    Also it might help if you state which version of Ruby and Rails you are using.

    0 讨论(0)
  • 2020-12-03 01:42

    The simplest way is render json: {foo: 'bar'}

    0 讨论(0)
提交回复
热议问题