如何写强大的params rails 4

时间:2013-11-23 10:37:28

标签: ruby-on-rails ruby-on-rails-4

我没有得到确切的解决方案来找到给定参数的强参数。

请帮我解决这个问题

"service"=>{"1"=>{"client_id"=>"testid", "client_secret"=>"testsecret"}, "2"=>{"client_id"=>"testkey", "client_secret"=>""}, "3"=>{"client_id"=>"", "client_secret"=>""}}

我试过

def service_params
  params.require(:service).permit(:id, :client_id, :client_secret)
end

我收到错误

Unpermitted parameters: 1, 2, 3

编辑:

我的表格是

  <%= form_for :service, :url => update_config_path, :html => { :class => "form-horizontal", :method => "put", :remote => true } do %>
    <% @services.each do |s| %>
      <%= fields_for "service[]", s do |service_field| %>
        <fieldset>
          <legend><%= s.name %></legend>
          <div class="form-group">
            <%= service_field.label :client_id, "Consumer Key", :class => "col-sm-2 control-label" %>
            <div class="col-sm-10">
              <%= service_field.text_field :client_id, :class => "form-control" %>
            </div>
          </div>
          <div class="form-group">
            <%= service_field.label :client_secret, "Consumer Secret", :class => "col-sm-2 control-label" %>
            <div class="col-sm-10">
              <%= service_field.text_field :client_secret, :class => "form-control" %>
            </div>
          </div>
        </fieldset>
      <% end %>
    <% end %>

    <%= submit_tag %>

  <% end %>

2 个答案:

答案 0 :(得分:0)

我还没有测试过,但这样的事情可能有用:

def service_params
  params.require(:service).map do |_, p|
    p.permit(:id, :client_id, :client_secret)
  end
end

答案 1 :(得分:0)

这样的事情可行:

def service_params
  params.require(:service).permit.tap do |whitelisted|
    whitelisted["1"] = params["1"]
    whitelisted["2"] = params["2"]
    whitelisted["3"] = params["3"]
  end
end