在ruby中使用params.require保存多个记录

时间:2013-11-22 23:57:38

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

我正在尝试更新多条记录。表单生成正常并提交正常,并发送数据。查找记录以进行更新,但不会保存任何数据。我在下面有我的控制器逻辑,表单逻辑和控制台转储。我试图复制Anthony Lewis put together,但我感觉我没有将正确的数据传递给params.require().permit()方法或正确定义class ConfigController < ApplicationController def edit @services = Service.all end def update params["service"].keys.each do |id| @service = Service.find(id.to_i) @service.update_attributes!(service_params) end redirect_to config_url end private def service_params params.require(:service).permit(:id, :client_id, :client_secret) end end 方法。在此先感谢您的帮助!

  <%= 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 %>

表格代码是:

Started PUT "/config" for 127.0.0.1 at 2013-11-22 15:44:08 -0800
Processing by ConfigController#update as JS
  Parameters: {"utf8"=>"✓", "service"=>{"1"=>{"client_id"=>"testid", "client_secret"=>"testsecret"}, "2"=>{"client_id"=>"testkey", "client_secret"=>""}, "3"=>{"client_id"=>"", "client_secret"=>""}}, "commit"=>"Save changes"}
  Service Load (0.3ms)  SELECT "services".* FROM "services" WHERE "services"."id" = $1 LIMIT 1  [["id", 1]]
Unpermitted parameters: 1, 2, 3
   (0.1ms)  BEGIN
   (0.1ms)  COMMIT
Unpermitted parameters: 1, 2, 3
{}
  Service Load (0.2ms)  SELECT "services".* FROM "services" WHERE "services"."id" = $1 LIMIT 1  [["id", 2]]
Unpermitted parameters: 1, 2, 3
   (0.1ms)  BEGIN
   (0.1ms)  COMMIT
Unpermitted parameters: 1, 2, 3
{}
  Service Load (0.2ms)  SELECT "services".* FROM "services" WHERE "services"."id" = $1 LIMIT 1  [["id", 3]]
Unpermitted parameters: 1, 2, 3
   (0.1ms)  BEGIN
   (0.1ms)  COMMIT
Unpermitted parameters: 1, 2, 3
{}
Redirected to http://localhost:3000/config
Completed 302 Found in 6ms (ActiveRecord: 1.1ms)

控制台显示:

{{1}}

5 个答案:

答案 0 :(得分:3)

我找到了解决方案,但也许并不是最好的解决方案。如果有人有更好的想法,请告诉我,我很乐意尝试一下!

在解决方案中,我更新了控制器update操作和service_params

我已将id传递给service_params并在fetch方法上调用require方法以获取正确的参数。我注意到在控制台中它会在保存每条记录时读取Unpermitted parameters: 1, 2, 3,表明params是一个数组,我也注意到在@Vijay的解决方案中他也尝试缩小了params。经过一些谷歌搜索和控制台记录后,我想出了下面的代码。

def update

  params["service"].keys.each do |id|
    @service = Service.find(id.to_i)
    @service.update_attributes!(service_params(id))
  end

  redirect_to config_url

end

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

您怎么看?

答案 1 :(得分:0)

试试这个......

params.require(:service).permit( id: [ :client_id, :client_secret ] )

答案 2 :(得分:0)

您的强参数行应

params.require(:service).permit( [:id, :client_id, :client_secret ] )

这允许值数组 http://guides.rubyonrails.org/action_controller_overview.html#more-examples

答案 3 :(得分:0)

尝试这个,它会起作用。

如果您遇到任何问题,请告诉我

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

http://blog.sensible.io/2013/08/17/strong-parameters-by-example.html

答案 4 :(得分:0)

请尝试:

def update
  Service.update(service_params.keys, service_params.values
end

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