简体   繁体   English

设置Rails以接收HTTP POST请求

[英]Setting Up Rails to Receive HTTP POST Request

Is setting up Rails to receive a HTTP POST request that sends information encoded in JSON form as easy as adding the following code to the sessions_controller? 将Rails设置为接收HTTP POST请求来发送以JSON格式编码的信息,就像将以下代码添加到session_controller一样容易吗? Or are there other steps involved? 还是涉及其他步骤?

def create
    if user = User.authenticate(params["email"], params["password"])
    session[:user_id] = user.id
    render:json => "{\"r\": \"t\"}" + req
else
    render :json => "{\"r\": \"f\"}"
end
end

Should be that easy, though you will need to add a route to your routes.rb file as well specifying POST as the HTTP verb and pointing it to sessions#create . 应该很简单,尽管您将需要向您的routes.rb文件添加一条路由, routes.rb POST指定为HTTP动词并将其指向sessions#create You also might want to use strong parameters just to validate what parameters are required and which you'll accept. 您可能还想使用强参数只是为了验证需要哪些参数以及您将接受哪些参数。 As a heads up, I'm not entirely sure what "{\\"r\\": \\"t\\"}" + req is supposed to represent. 请注意,我不完全确定"{\\"r\\": \\"t\\"}" + req应该代表什么。 It looks like req would be undefined in this case, but perhaps you're just omitting some code. 看起来req在这种情况下是不确定的,但是也许您只是省略了一些代码。 Lastly, render :json => ... is sort of the old way of including a hash. 最后, render :json => ...是一种包含哈希的旧方法。 I believe as of Ruby 2 the standard is something more like render json: ... . 我相信从Ruby 2开始,标准更像是render json: ... Hopefully that helps. 希望有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM