2014-05-14 61 views
8

我是Ruby on Rails的全新产品。 我使用的是ActiveAdmin和我有创造ADMINUSERActiveAdmin ForbiddenAttributesError

::加载ActiveModel在ForbiddenAttributesError管理员::问题AdminUsersController#创建 ::加载ActiveModel ForbiddenAttributesError

请求

参数:

  • { “UTF8”=> “✓”,

  • “authenticity_token”=> “NVV ++ 6GNTdA/nDzw1iJ6Ii84pZPcv2mzg0PK2Cg9Ag0 =”,

  • “admin_user”=> { “电子邮件”=> “[email protected]”},

  • “提交”=> “创建管理员用户”} *


条的Rails 4.1.0

activeadmin 1.0.0

红宝石2.1


应用/管理/ 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 

应用程序/楷模/ 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 

的Gemfile

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 

配置/环境/ 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 

回答

34

Rails 4使用强大的参数,其移动属性白名单从模型到控制器。有必要指定您想要保存在数据库中的属性。您没有允许代码中的属性,这就是您收到ActiveModel::ForbiddenAttributesError的原因。

参考的ActiveAdmin : Setting up Strong Parameters

的文档,你可以通过以下方式建立强大的参数,使用permit_params方法覆盖createupdate动作时,它创建了一个名为permitted_params方法,使用这种方法:

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

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

+2

非常感谢!现在为我解决;) – dPanda13

+1

像3小时的谷歌和试验后,我觉得这个,它的工作!非常感谢! –

1

你看到的是新版本Rails的安全特性。您必须为属性创建白名单,这些属性可以由用户输入的参数更新。否则,您将不得不手动设置每个值。

这里的白名单某则params的一个样本:

ActiveAdmin.register Post do 
    permit_params :title, :content, :publisher_id 
end 

见关于这一主题的ActiveAdmin文档: https://github.com/gregbell/active_admin/blob/master/docs/2-resource-customization.md#setting-up-strong-parameters

相关问题