从这个参数创建变量时,结果总是为零,为什么?

时间:2013-07-05 12:44:54

标签: ruby-on-rails ruby ruby-on-rails-3

我正在提交表单以创建我的SpecialDeal模型的新记录。提交的params看起来像这样:

@_params    
{"utf8"=>"✓", "authenticity_token"=>"cl1SwnHOum8d/kiGnwkDsamG5IMbmdnoeFvlY11KpKc=", "special_deal"=>{"title"=>"5", "provider"=>"5", "description"=>"5", "deal_price"=>"", "conditions"=>"", "expires"=>"07/18/2013 14:38:18", "excerpt"=>"", "original_price"=>"", "phone_number"=>"", "street"=>"", "city"=>"", "postal_code"=>"", "state"=>"", "country"=>""}, "commit"=>"Create Special deal", "action"=>"create", "controller"=>"special_deals"}

然而,当我尝试从expires参数中创建一个变量...

def create
  expiration = params[:expires]

该变量总是将自己设为nil!

Local Variables

expiration: nil

为什么会这样?

2 个答案:

答案 0 :(得分:3)

expires在内部哈希中,位于'special_deal'键下,请尝试

def create
  expiration = params[:special_deal][:expires]

我假设您正在使用rails params

答案 1 :(得分:2)

正如我之前评论过的那样。现在添加它作为我的答案;)

def create
  expiration = params["special_deal"]["expires"]
  # Your code goes here
end