Rails 5,访问参数hash

时间:2017-03-16 18:58:53

标签: parameters ruby-on-rails-5 strong-parameters

我想我在这里失去它...所以我有一个params哈希,我想在控制器中使用:

参数: {“utf8”=>“✓”,“_ method”=>“patch”,“authenticity_token”=>“etcetc”,“scan”=> {“1”=> {“scan_id”=>“40”,“organisation_id”=>“1”,“select_scan”=>“false”,“role”=>“”},“2 “=> {”scan_id“=>”40“,”organisation_id“=>”2“,”select_scan“=>”false“,”role“=>”“},”3“=> ; {“scan_id”=>“40”,“organisation_id”=>“3”,“select_scan”=>“false”,“role”=>“”},“number”=>“222 “,”“description”=>“nice!”,“expert”=>“for dummies”,“detralization”=>“0”},“commit”=>“更新扫描”,“id”= > “中40”}

现在,如果返回我的视图一个实例变量,值为:

@q = params[:id]
正如预期的那样,这给了我'40'。

@q = params['id']

也给我'40'

但是,这是我可以访问的唯一参数值吗?每个其他值都给我零或错误,如:

 @q = params[:scan][:number] # => undefined method `[]' for nil:NilClass
@q = params['scan']['1'][:role] # => undefined method `[]' for nil:NilClass
    @q = params[:scan].to_a # => []
@q = params[:scan].class # => NilClass

我读过有关强烈的参数,所有参数都被允许,但仍然没有返回值?

1 个答案:

答案 0 :(得分:-1)

只是一个但是我可以看到,你的哈希结构在引号下("")并且你试图用符号(:)进行访问它.whithout .with_indifferent_access

e.g:

@q = params['scan']['number'] 
@q = params['scan']['1']['role'] 
@q = params['scan'].to_a
@q = params['scan'].class

试一试。