2014-05-14 16 views
8

Tôi là một thương hiệu mới cho Ruby on Rails. Tôi đang sử dụng một ActiveAdmin và tôi có một vấn đề với việc tạo ra một AdminUserActiveAdmin ForbiddenAttributesError

ActiveModel :: ForbiddenAttributesError trong Admin :: AdminUsersController # tạo ActiveModel :: ForbiddenAttributesError

Yêu cầu

Tham số:

  • { "utf8" => "✓",

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

  • "admin_user" => { "email" => "[email protected]"},

  • "cam kết" => "Tạo người dùng quản trị"} *


Rails 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/mô hình/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 

config/môi trường/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 

Trả lời

34

Rails 4 sử dụng các thông số mạnh mẽ, trong đó di chuyển danh sách trắng thuộc tính từ mô hình để bộ điều khiển. Nó là cần thiết để xác định các thuộc tính mà bạn muốn được lưu trong cơ sở dữ liệu. Bạn đã không cho phép các thuộc tính trong mã của bạn, đó là lý do tại sao bạn nhận được ActiveModel::ForbiddenAttributesError.

Tham khảo tài liệu của ActiveAdmin : Setting up Strong Parameters

Bạn có thể các thông số thiết lập mạnh mẽ theo cách sau, sử dụng permit_params phương pháp mà tạo ra một phương pháp gọi là permitted_params, sử dụng phương pháp này khi trọng create hoặc update hành động:

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

Thay thế :attr1, :attr2, v.v ... với tên thuộc tính thực tế mà bạn muốn đưa vào danh sách cho phép. Ví dụ: :email

+2

Cảm ơn rất nhiều! Bây giờ được giải quyết cho tôi;) – dPanda13

+1

Sau 3 giờ sử dụng Google và thử nghiệm tôi tìm thấy điều này và nó đã hoạt động! Cám ơn rất nhiều! –

1

Điều bạn đang thấy là tính năng bảo mật của các phiên bản Rails mới hơn. Bạn sẽ phải tạo một danh sách trắng cho các thuộc tính có thể được cập nhật bởi các tham số được người dùng nhập vào. Nếu không, bạn sẽ phải đặt từng giá trị theo cách thủ công.

Dưới đây là một mẫu danh sách trắng params chắc chắn:

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

Xem các tài liệu ActiveAdmin về đề tài này: https://github.com/gregbell/active_admin/blob/master/docs/2-resource-customization.md#setting-up-strong-parameters

Các vấn đề liên quan