2014-05-14 5 views
8

मैं रेल पर रूबी के लिए एक नया ब्रांड हूं। मैं एक ActiveAdmin ActiveModel :: ForbiddenAttributesError उपयोग कर रहा हूँ और मैं एक AdminUserActiveAdmin ForbiddenAttributesError

ActiveModel :: व्यवस्थापक में ForbiddenAttributesError :: बनाने के साथ एक समस्या है AdminUsersController # बनाने

अनुरोध

पैरामीटर:

  • { "UTF8" => "✓",

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

  • "admin_user" => { "ईमेल" => "[email protected]"},

  • "प्रतिबद्ध" => "व्यवस्थापक उपयोगकर्ता बनाएँ"} *


रेल 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 

config/वातावरण/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

के दस्तावेज़ देखें आप कर सकते हैं निम्नलिखित तरीके से सेटअप मजबूत पैरामीटर, जब create या update कार्यों अधिभावी जो एक विधि permitted_params कहा जाता है बनाता है, इस विधि का उपयोग permit_params विधि का उपयोग कर:

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

:attr1, :attr2, आदि को वास्तविक विशेषता नामों के साथ बदलें जिन्हें आप श्वेतसूची में रखना चाहते हैं। उदाहरण के लिए: :email

+2

धन्यवाद एक बहुत! अब मेरे लिए हल किया गया है;) – dPanda13

+1

Google के 3 घंटे और प्रयोग करने के बाद मुझे यह पता चला और यह काम किया! बहुत बहुत धन्यवाद! –

1

जो आप देख रहे हैं वह रेल के नए संस्करणों की सुरक्षा सुविधा है। आपको उन गुणों के लिए श्वेतसूची बनाना होगा जिन्हें उपयोगकर्ता द्वारा दर्ज किए गए पैरा द्वारा अपडेट किया जा सकता है। अन्यथा, आपको प्रत्येक मान मैन्युअल रूप से सेट करना होगा।

यहाँ श्वेत सूची बनाती कुछ पैरामीटर का एक नमूना है:

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