find_by不适用于传递的params项

时间:2018-08-07 14:26:43

标签: ruby-on-rails ruby activerecord

为什么行得通?

<server>
  <featureManager>
    <feature>ejbPersistentTimer-3.2</feature>
    ... your other features
  </featureManager>

  <dataSource id="DefaultDataSource">
    <containerAuthData user="user1" password="pwd1"/>
    <jdbcDriver libraryRef="DerbyLib"/>
    <properties.derby.embedded createDatabase="create" databaseName="timerdb"/>
  </dataSource>
  <library id="DerbyLib">
    <fileset dir="C:/Drivers/derby" includes="derby.jar"/>
  </library>
</server>

上面代码的输出是

class TransfersController < ApplicationController
  def create
    @receiver = Wallet.find_by(number: "e27690b243dc249638b202c930dc022f")
    @sender = Wallet.find_by(number: "3e6c234504e407309d7d91e67f65f5fb")
    render :json => {:receiver => @receiver, :sender => @sender, :params => params}
end
end

但这不是吗?接收者和发送者对象均为空

{"receiver":{"id":1,"owner":1,"number":"e27690b243dc249638b202c930dc022f","money":100,"status":0,"reason":null,"created_at":"2018-08-07T13:46:52.015Z","updated_at":"2018-08-07T13:46:52.015Z"},"sender":{"id":2,"owner":1,"number":"3e6c234504e407309d7d91e67f65f5fb","money":100,"status":0,"reason":null,"created_at":"2018-08-07T13:56:22.097Z","updated_at":"2018-08-07T13:56:22.097Z"},"params":{"utf8":"✓","authenticity_token":"9xlWBswszN6cZSYFeA24TEMDfJheiw+0ZLObdFRw/SYVT0mwDlJu2XMYktvnF0X+n+7uUX8G7iFd07ndNa98Vg==","transfer":{"sender":"e27690b243dc249638b202c930dc022f","receiver":"3e6c234504e407309d7d91e67f65f5fb","summa":"5","comment":"dfhdhfdhf"},"commit":"Отправить","controller":"transfers","action":"create"}}

JSON输出为

class TransfersController < ApplicationController
  def create
    @receiver = Wallet.find_by(number: params['receiver'])
    @sender = Wallet.find_by(number: params['sender'])
    return :json => {:receiver => @receiver, :sender => @sender, :params => params}
end
end

1 个答案:

答案 0 :(得分:1)

因为您的标识符嵌套在:transferparams下方。

尝试以下方法:

@receiver = Wallet.find_by(number: params['transfer']['receiver'])
@sender = Wallet.find_by(number: params['transfer']['sender'])