在controller中可以通过params[:字段]的方式来获取发送过来的数据,
无论他们是来自get中的url中?后面,还是来自post数据,rails不区分这两种参数
context "#test" do
con = AccountsController.new
before do
@params = {mobile_number: '128888888888'}
allow(JrOpenClient::Api::Login).to receive(:send_sms).and_return({status: 0, msg: 'ok'})
end
it do
#Controller不需要传参数时
result = con.send_sms()
puts result
end
end
context "xxx" do
before do
@params = {mobile_number: '128888888888'}
end
it do
#该Controller需要相应参数时
#此处用get、post均可
post :send_sms, params: @params
response_hash = JSON.parse(response.body).deep_symbolize_keys
expect(response_hash[:status]).to eq 1
expect(response_hash[:status_text]).to eq "手机号格式错误"
end
end
本文介绍在Rails框架中,如何通过params[:字段]的方式统一处理GET和POST请求中的参数,不论参数是来源于URL的查询字符串还是POST数据。文章通过具体示例展示了在不同场景下,如Controller是否需要特定参数时,如何进行参数的获取与验证。

6826

被折叠的 条评论
为什么被折叠?



