【问题标题】:Unpermitted parameters for Dynamic Forms in Rails 4Rails 4 中动态表单的不允许参数
【发布时间】:2013-10-11 22:42:58
【问题描述】:

我是 Rails 新手,并基于此构建了一些东西

http://railscasts.com/episodes/403-dynamic-forms

但我在附加字段中存储数据时遇到问题... 我有一个 ProductType 对象,它有许多 ProductField 对象。 ProductField 对象也属于 ProductType,而 Product 对象属于 ProductType。

因此,可以通过构造函数 ProductType 轻松添加新的动态字段,但是当我尝试通过 Product 控制器在该字段中设置数据时,没有任何反应。

我确定问题与使用强参数有关,但修复描述的 herehere 没有帮助。

product.rb

class Product < ActiveRecord::Base
    belongs_to :product_type
    serialize :properties, Hash
end

product_type.rb

class ProductType < ActiveRecord::Base
    has_many :fields, class_name: "ProductField"
    accepts_nested_attributes_for :fields, allow_destroy: true
end

product_field.rb

class ProductField < ActiveRecord::Base
    belongs_to :product_type
end

products_controller.rb

class ProductsController < ApplicationController
    def new
    @product = Product.new(product_type_id: params[:product_type_id])
    end
    def product_params
    params.require(:product).permit(:name, :price, :product_type_id, {:properties => []})
    end

product_type_controller.rb

class ProductTypesController < ApplicationController
    def product_type_params
    params.require(:product_type).permit(:name, fields_attributes: [:id, :name, :field_type, :required, :product_type_id])
    end

在控制台日志中: 不允许的参数:属性

Started PATCH "/products/4" for 127.0.0.1 at 2013-10-04 22:54:59 +0400
Processing by ProductsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"my3ra60OUXexmmguk2eqRetizx3tWPMq04Z2PnODJMQ=", "product"=>{"product_type_id"=>"1", "name"=>"Product1", "properties"=>{"gjfghjf"=>"123", "123"=>[""]}, "price"=>"10"}, "commit"=>"Update Product", "id"=>"4"}
Product Load (0.3ms)  SELECT "products".* FROM "products" WHERE "products"."id" = ? LIMIT 1  [["id", "4"]]
Unpermitted parameters: properties

P.S:也许有人在观看播客时遇到了类似的问题?

【问题讨论】:

  • 您可以发布您的表单代码吗?我看到你得到这个:"properties"=&gt;{"gjfghjf"=&gt;"123", "123"=&gt;[""]} 在你的返回参数中。属性以两个项目的散列形式出现,其中一个是数组。在您的 product_params 中,它期望根据其编写方式允许的标量值数组。这个 Railscast 最初是使用 Rails 4 完成的吗?
  • 如果您想测试在表单代码或permit 语句中如何构造参数是否存在问题,您可以这样做:params.require(:product).permit!。它会将所有内容列入白名单。有利于测试,但对安全性非常不利,因此您需要在某个时候对其进行整理。
  • 是的,params.require(:product).permit!确实有效,但是对安全性非常不利,您是对的..

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


【解决方案1】:

如果你想返回一个嵌套哈希作为参数,你必须在permit 中命名数组中的键。

class ProductsController < ApplicationController
def new
@product = Product.new(product_type_id: params[:product_type_id])
end
def product_params
params.require(:product).permit(:name, :price, :product_type_id, {:properties => [:foo, :bar, :id]})
end

如果您正在动态生成密钥并且无法将它们编码到permit 语句中,那么您需要使用这种样式:

def product_params
  params.require(:product).permit(:name, :price, :product_type_id).tap do |whitelisted|
    whitelisted[:properties] = params[:product][:properties]
  end
end

对于新用户来说,这不是最友好的代码,我刚刚完成了 UW 的 3 course rails 证书,他们甚至没有覆盖.tap

这不是我的工作,我仍然只是像这样理解.permit的更深层次的部分。这是我使用的博客条目:Strong Parameters by Example

【讨论】:

  • 那么上面的代码没有任何修改就可以工作了吗?如果需要任何模组,请在此处留下评论,我会更改它,以便答案更准确。如果它开箱即用完美,那该死的!有点幸运,因为我只是根据我对 Strong Params 的了解和我所读到的内容进行推断。
猜你喜欢
  • 2013-08-01
  • 2023-03-23
  • 2013-07-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多