访问ActionController参数hash的值

时间:2017-09-12 17:31:49

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

我在从4.2升级到5.1的应用中遇到参数问题。我已经允许我的参数,但正如文档所述,我收回了哈希数组的对象,但似乎无法访问它的值。我怎样才能获得这个对象的价值?

    {"_method"=>"delete",   "authenticity_token"=>"Z6ZqriiuXu6ODDqhGgocGiaN12rjKD6pUB6n/2v+CABZDAjwLzwczsMM3nM8f0PI0nww43o5mlC35HK+9PVa8w==",
    "domain_name"=>"test.testmodule2.com.",
    "hosted_zone_id"=>"/hostedzone/XXXXXXXXXX",
    "ttl"=>"3600",
    "type"=>"A",
    "value"=>[{"value"=>"1.1.1.1"}],
    "id"=>"/hostedzone/XXXXXXXXXX"}

def record_params
    params.permit!([:hosted_zone_id, :domain_name, :type, :ttl, :alias, :value]).to_h!
end


def destroy
    value = params[:value]
    # returns [<ActionController::Parameters {"value"=>"1.1.1.1"} permitted: true>]
    # would like it to return [{"value"=>"1.1.1.1"}]
end

1 个答案:

答案 0 :(得分:1)

如果该哈希值是params[:value]的值,则访问内部的value键,如:

params[:value] = {
  "_method"=>"delete",   
  "authenticity_token"=>"...",
  "domain_name"=>"test.testmodule2.com.",
  "hosted_zone_id"=>"/hostedzone/XXXXXXXXXX",
  "ttl"=>"3600",
  "type"=>"A",
  "value"=>[{"value"=>"1.1.1.1"}],
  "id"=>"/hostedzone/XXXXXXXXXX"
}

params['value']['value'][0]['value']
# => "1.1.1.1"
params['value']['value'][0].keys
# => ["value"]