gpt4 book ai didi

ruby-on-rails-4 - Rails 4 和 Active Admin:ActiveModel::ForbiddenAttributesError

转载 作者:行者123 更新时间:2023-12-04 16:53:01 24 4
gpt4 key购买 nike

完整的错误如下:

ActiveModel::ForbiddenAttributesError in Admin::ProductsController#create

我的产品型号只有 nameprice . 为什么是 commit一个参数? 当我单击管理仪表板中的“创建产品”按钮时,这是参数输出:
Parameters:

{"utf8"=>"✓",
"authenticity_token"=>"6/pCeklsaik4sYF5h8+WRPddkH7wJn9ZJHd6YLaaNuc=",
"product"=>{"name"=>"Black Shirt Male",
"price"=>"25"},
"commit"=>"Create Product"}

从我阅读其他 Stack Overflow 帖子中收集的信息来看,您需要在 Rails 4 中使用强参数而不是 attr_accessible ,这是在我搭建产品模型时为我完成的。在我的 create在 Products Controller 中操作,我有:
@product = Product.new(product_params)

而 product_params 定义为:
def product_params
params.require(:product).permit(:name, :price)
end

创建模型时我没有做任何花哨的事情,在我的 Gemfile 中,我使用以下内容作为 Rails 4 的建议文档:
gem 'stripe', :git => 'https://github.com/stripe/stripe-ruby'

当我通过 Active Admin 仪表板创建新产品时,为什么会出现此错误?对此事的任何意见表示赞赏。

最佳答案

好吧想通了。我不确定这是否是“正确”的方式,但正在创建产品。

在 app/admin/product.rb 我做了:

permit_params :list, :of, :attributes, :on, :model, :name, :price

在哪里
permit_params :list, :of, :attributes, :on, :model

最初被注释掉了。所以我刚刚添加了 :name:price

关于ruby-on-rails-4 - Rails 4 和 Active Admin:ActiveModel::ForbiddenAttributesError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22521049/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com