2014-05-02 10 views
29

मैं रेल के लिए कुछ नया हूं और मैं उपयोगकर्ता लॉगिन बनाने की कोशिश कर रहा हूं। मैं ट्यूटोरियल के माध्यम से here मिला। अंत में यह मुझे बड़े पैमाने पर असाइनमेंट के लिए "attr_accessible" जोड़ दिया था।अपरिभाषित विधि attr_accessible

undefined method `attr_accessible' for #<Class:0x007ff70f276010> 

मैं इस post कि मैं neeed < ActiveRecord :: बेस पर देखा: लेकिन जब मुझे लगता है कि मैं निम्नलिखित त्रुटि मिली थी। लेकिन मेरे पास यह शामिल है। मेरे उपयोगकर्ता मॉडल के लिए कोड यहां दिया गया है:

class User < ActiveRecord::Base 

    attr_accessor :password 
    EMAIL_REGEX = /\A[A-Z0-9._%+-][email protected][A-Z0-9.-]+\.[A-Z]{2,4}\z/i 
    validates :username, :presence => true, :uniqueness => true, :length => { :in => 3..20 } 
    validates :email, :presence => true, :uniqueness => true, :format => EMAIL_REGEX 
    validates :password, :confirmation => true #password_confirmation attr 
    validates_length_of :password, :in => 6..20, :on => :create 
    before_save :encrypt_password 
    after_save :clear_password 
    attr_accessible :username, :email, :password, :password_confirmation 

    def encrypt_password 
    if password.present? 
     self.salt = BCrypt::Engine.generate_salt 
     self.encrypted_password= BCrypt::Engine.hash_secret(password, salt) 
    end 
    end 

    def clear_password 
    self.password = nil 
    end 

end 

इस समस्या का कारण बनने के बारे में कोई अन्य विचार वास्तव में सराहना की जाएगी, धन्यवाद!

संपादित करें: रेल 4.1 पर। ऐसा लगता है कि यह अब लागू नहीं होता है। धन्यवाद fotanus

+4

[यहां पढ़ें] (http://stackoverflow.com/questions/17371334/how-is-attr-accessible-used-in-rails-4)। अपने प्रश्न को सही रेल संस्करण के साथ ठीक करें, क्योंकि यह इस प्रश्न के लिए महत्वपूर्ण है। – fotanus

उत्तर

74

कोई बड़े पैमाने पर काम रेल strong parameters 4.1

बजाय अपने मॉडल में attr_accessible :username, :email, :password, :password_confirmation होने के

, का उपयोग के लिए अनुमति दी। आप अपने UsersController में करेंगे:

def user_params 
     params.require(:user).permit(:username, :email, :password, :password_confirmation) 
    end 

फिर अपने नियंत्रक कार्यों में user_params विधि कॉल।

15

कोई बड़े पैमाने पर काम रेल 4.1

आप कुछ इस तरह की कोशिश करनी होगी के लिए अनुमति दी।

class Person 
    has_many :pets 
    accepts_nested_attributes_for :pets 
end 

class PeopleController < ActionController::Base 
    def create 
    Person.create(person_params) 
    end 

    ... 

    private 

    def person_params 
     # It's mandatory to specify the nested attributes that should be whitelisted. 
     # If you use `permit` with just the key that points to the nested attributes hash, 
     # it will return an empty hash. 
     params.require(:person).permit(:name, :age, pets_attributes: [ :name, :category ]) 
    end 
end 

देखें

https://github.com/rails/strong_parameters

2

सुनिश्चित करें कि आप मणि 'protected_attributes' स्थापित कर रहे हैं, कि यह मणि अपने gemfile में मौजूद है और bundle install टर्मिनल चलाते हैं। फिर अपने सर्वर को पुनरारंभ करें।

संबंधित मुद्दे