gpt4 book ai didi

ruby - ActiveAdmin ForbiddenAttributesError

转载 作者:数据小太阳 更新时间:2023-10-29 06:38:24 25 4
gpt4 key购买 nike

我是 Ruby on Rails 的新手。我正在使用 ActiveAdmin,但在创建 AdminUser 时遇到问题

ActiveModel::AdminUsersController 中的 ForbiddenAttributesError#createActiveModel::ForbiddenAttributesError

请求

参数:

  • {"utf8"=>"✓",

  • "authenticity_token"=>"nvV++6GNTdA/nDzw1iJ6Ii84pZPcv2mzg0PK2Cg9Ag0=",

  • "admin_user"=>{"email"=>"admin2@example.com"},

  • "commit"=>"创建管理员用户"}*


轨道 4.1.0

activeadmin 1.0.0

ruby 2.1


app/admin/admin_user.rb

ActiveAdmin.register AdminUser do
index do
column :email
column :current_sign_in_at
column :last_sign_in_at
column :sign_in_count
default_actions
end

form do |f|
f.inputs "Admin Details" do
f.input :email
end
f.actions
end
end

app/models/admin_user.rb

class AdminUser < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable,
:recoverable, :rememberable, :trackable, :validatable

after_create { |admin| admin.send_reset_password_instructions }

def password_required?
new_record? ? false : super
end
end

gem 文件

source 'https://rubygems.org'

gem 'rails', '4.1.0'
gem 'sqlite3'
gem 'sass-rails', '~> 4.0.3'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.0.0'
gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 2.0'
gem 'activeadmin', github: 'gregbell/active_admin'
gem 'polyamorous', github: 'activerecord-hackery/polyamorous'
gem 'ransack', github: 'activerecord-hackery/ransack'
gem 'formtastic', github: 'justinfrench/formtastic'
gem 'devise'

gem 'sdoc', '~> 0.4.0', group: :doc

config/environments/development.rb

Rails.application.configure do
# Settings specified here will take precedence over those in config/application.rb.

# In the development environment your application's code is reloaded on
# every request. This slows down response time but is perfect for development
# since you don't have to restart the web server when you make code changes.
config.cache_classes = false

# Do not eager load code on boot.
config.eager_load = false

# Show full error reports and disable caching.
config.consider_all_requests_local = true
config.action_controller.perform_caching = false

# Don't care if the mailer can't send.
config.action_mailer.raise_delivery_errors = false

# Print deprecation notices to the Rails logger.
config.active_support.deprecation = :log

# Raise an error on page load if there are pending migrations.
config.active_record.migration_error = :page_load

# Debug mode disables concatenation and preprocessing of assets.
# This option may cause significant delays in view rendering with a large
# number of complex assets.
config.assets.debug = true

# Adds additional error checking when serving assets at runtime.
# Checks for improperly declared sprockets dependencies.
# Raises helpful error messages.
config.assets.raise_runtime_errors = true

# Raises error for missing translations
# config.action_view.raise_on_missing_translations = true

# Sending emails works
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
end

最佳答案

Rails 4 使用强参数,将属性白名单从模型转移到 Controller 。有必要指定您希望保存在数据库中的属性。您不允许代码中的属性,这就是您收到 ActiveModel::ForbiddenAttributesError 的原因。

引用的文档ActiveAdmin : Setting up Strong Parameters

您可以通过以下方式设置强参数,使用 permit_params 方法创建一个名为 permitted_pa​​rams 的方法,在覆盖 create 时使用此方法或 更新 操作:

ActiveAdmin.register AdminUser do
## ...
permit_params :attr1, :attr2 ## Add this line
end

:attr1:attr2 等替换为您要列入白名单的实际属性名称。例如::email

关于ruby - ActiveAdmin ForbiddenAttributesError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23655366/

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