2011-04-25 96 views
0

我需要从表单访问hashmapped值。这是我看到我倾倒表单元素,但我不知道我怎么能在控制器访问它们:Ruby on Rails:访问hashmap表单元素

{"Cart"=>{"exclude_discount"=>"1", 
"only_one_product"=>"0", 
"include_surcharge"=>"1", 
"include_timesheet_date"=>"1"}} 

我试图访问这些控制器使用

params[:Cart[only_one_product]] 
and params[:Cart[:only_one_product]] 
and params[:Cart["only_one_product"]] 

都失败。任何快速的帮助真的很感激。

回答

4

你想params[:Cart][:exclude_discount]

既然是哈希散列,你需要先获得外散“PARAMS [:车]”的元素,然后获取哈希的内部元件,[:exclude_discount]

3
params[:Cart][:only_one_product] 

是访问它的正确方法。

1
params["Cart"]["only_one_product"] 
1

朗风格

cart = params[:Cart] # get hash 
is_only_one_product = cart[:is_only_one_product] # get hash key-value 

短花柱

is_only_one_product = params[:Cart][:is_only_one_product] # get hash key-value