Ruby Controller中方法的调用

本文介绍在Rails框架中,如何通过params[:字段]的方式统一处理GET和POST请求中的参数,不论参数是来源于URL的查询字符串还是POST数据。文章通过具体示例展示了在不同场景下,如Controller是否需要特定参数时,如何进行参数的获取与验证。

在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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值