2015-03-06 11 views
5

इसके लिए कोई कामकाजी उत्तर नहीं दिख रहा है। मैं रेल ट्यूटोरियल के अध्याय 10, धारा 10.1.2 पर हूं और मेलर पूर्वावलोकन काम नहीं कर रहा है। त्रुटि से निपटने वाले सभी उत्तरों को ट्यूटोरियल के विभिन्न वर्गों से संबंधित है, और मुझे लगता है कि मैं जो त्रुटि कर रहा हूं वह मुझे चेहरे पर देख रहा है। मैंने ट्यूटोरियल से संबंधित फाइलों में कोड को कॉपी और पेस्ट किया है और अब तक जो मैंने टाइप किया है और ट्यूटोरियल में क्या था, उसके बीच एक अंतर देखने में सक्षम नहीं है। अब तक, फ़ंक्शन परिभाषाओं से user तर्क जोड़ने या निकालने के सुझाव दिए गए हैं, लेकिन इसने समस्या हल नहीं की है। त्रुटि को ट्रिगर करने वाला यूआरएल http://localhost:3000/rails/mailers/user_mailer/account_activation है। http://localhost:3000/rails/mailers/user_mailer/ कोई समस्या नहीं है, और न ही http://localhost:3000/rails/mailers/user_mailer/password_reset है (जिसे मैंने अभी तक कोई अनुकूलन नहीं किया है)। मैं क्या खो रहा हूँ?रेल में NoMethodError :: MailersController # पूर्वावलोकन अपरिभाषित विधि `activation_token = 'शून्य के लिए: NilClass

NoMethodError in Rails::MailersController#preview 
    undefined method `activation_token=' for nil:NilClass 

निकाले स्रोत:

def account_activation 
    user = User.first 
    user.activation_token = User.new_token # highlighted line 
    UserMailer.account_activation(user) 
end 

मैं क्या बता सकते हैं, यहाँ शामिल फ़ाइलें हैं:

यहाँ त्रुटि है

user_mailer.rb:

 class UserMailer < ApplicationMailer 

     # Subject can be set in your I18n file at config/locales/en.yml 
     # with the following lookup: 
     # 
     # en.user_mailer.account_activation.subject 
     # 
     def account_activation(user) 
     @user = user 
     mail to: user.email, subject: "Account activation" 
     end 

     def password_reset 
     @greeting = "Hi" 

     mail to: "[email protected]" 
     end 
    end 

user_mailer_prev iew.rb:

 # Preview all emails at http://localhost:3000/rails/mailers/user_mailer 
    class UserMailerPreview < ActionMailer::Preview 

     # Preview this email at http://localhost:3000/rails/mailers/user_mailer/account_activation 
     def account_activation 
     user = User.first 
     user.activation_token = User.new_token 
     UserMailer.account_activation(user) 
     end 

     # Preview this email at http://localhost:3000/rails/mailers/user_mailer/password_reset 
     def password_reset 
     UserMailer.password_reset 
     end 

    end 

development.rb:

Rails.application.configure do 

     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 = true 
     config.action_mailer.delivery_method = :test 
     host = 'localhost:3000' 
     config.action_mailer.default_url_options = { host: host } 

     # 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 

     # Asset digests allow you to set far-future HTTP expiration dates on all assets, 
     # yet still be able to expire them through the digest params. 
     config.assets.digest = 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 
    end 

और पूर्णता के लिए, यहाँ विचारों और मेरे उपयोगकर्ता मॉडल हैं:

account_activation.html.erb:

<h1>Sample App</h1> 

    <p>Hi <%= @user.name %>,</p> 

    <p> 
    Welcome to the Sample App! Click on the link below to activate your account: 
    </p> 

    <%= link_to "Activate", edit_account_activation_url(@user.activation_token, 
                 email: @user.email) %> 

account_activation.text.erb: हाय <% = @ user.name%>,

Welcome to the Sample App. Click on the link below to activate your account: 

    <%= edit_account_activation_url(@user.activation_token, email: @user.email) %> 

user.rb:

class User < ActiveRecord::Base 
     attr_accessor :remember_token, :activation_token 
     before_save :downcase_email 
     before_create :create_activation_digest 
     validates :name, presence: true, length: { maximum: 50 } 
     VALID_EMAIL_REGEX = /\A[\w+\-.][email protected][a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i 
     validates :email, presence: true, length: { maximum: 255 }, 
         format: { with: VALID_EMAIL_REGEX }, 
         uniqueness: { case_sensitive: false } 
     has_secure_password 
     validates :password, length: { minimum: 6 }, allow_blank: true 

     # Returns the hash digest of the given string. 
     def User.digest(string) 
     cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST : 
                 BCrypt::Engine.cost 
     BCrypt::Password.create(string, cost: cost) 
     end 

     # Returns a random token 
     def User.new_token 
     SecureRandom.urlsafe_base64 
     end 

     # Remembers a user in the database for use in persistent sessions 
     def remember 
     self.remember_token = User.new_token 
     update_attribute(:remember_digest, User.digest(remember_token)) 
     end 

     # Returns true if the given token matches the digest. 
     def authenticated?(remember_token) 
     return false if remember_digest.nil? 
     BCrypt::Password.new(remember_digest).is_password?(remember_token) 
     end 

     # Forgets a user. 
     def forget 
     update_attribute(:remember_digest, nil) 
     end 

     private 

     # Converts email to all lower-case. 
     def downcase_email 
      self.email = email.downcase 
     end 

     # Creates and assigns the activation token and digest. 
     def create_activation_digest 
      self.activation_token = User.new_token 
      self.activation_digest = User.digest(activation_token) 
     end 
    end 

उत्तर

5

वाह, बहुत कुछ के डिबगिंग और भ्रम का एक बहुत आसान समस्या की खोज के लिए: मैं लॉग इन नहीं किया गया था, और के लिए त्रुटि संदेश के किसी भी प्रकार से परिभाषित नहीं किया था वे उस पृष्ठ तक पहुंचने का प्रयास कर रहे हैं, यदि वे लॉग इन नहीं थे (इसलिए user.activation_token विधि Nil:NilClass त्रुटि को ट्रिगर करने वाली विधि)। ऐसा लगता है कि टीडीडी एक टन में मदद क्यों कर सकता है इसका एक अच्छा उदाहरण लगता है।

3

सुनिश्चित नहीं है कि आपका लॉग इन किया गया था, लेकिन कोई उपयोगकर्ता नहीं था।

क्या आपने bundle exec rake db:seed को पॉप्युलेट करने के लिए चलाया था? इसके अलावा अपने सांत्वना जाँच में अपने डेटाबेस आबादी यदि अपेक्षा के अनुरूप देखने के लिए ... अगर यह अपने "seed.rb" फ़ाइल चेकआउट नहीं किया है और अनुभाग जहां उन बनाई गई हैं में यह सुनिश्चित करें कि activated: true मौजूद है, इस प्रकार की तरह:

User.create!(name: "Example User", 
      email: "[email protected]", 
      password: "foobar", 
      password_confirmation: "foobar", 
      admin: true, 
     ** activated: true, 
      activated_at: Time.zone.now) 

ऊपर उल्लिखित त्रुटि, 'activation_token=' for nil:NilClass वास्तव में ऐसा प्रतीत होता है क्योंकि कोई उपयोगकर्ता नहीं थे।

और जब आपने बताया कि "लॉग इन" हो सकता है तो आप वास्तव में फिर से पंजीकृत हो सकते हैं, इस प्रकार इस सुविधा को काम करने के लिए आवश्यक उपयोगकर्ता बनाना। उम्मीद है की यह मदद करेगा!

0

यदि यह मेरे जैसा ही त्रुटि है, तो आपके डेटा बेस में डेटा और उपयोगकर्ता नहीं है। सबसे पहले अगली पंक्ति की तुलना में शून्य को पुनर्प्राप्त करें। अपनी बीज फ़ाइल की जांच करें और आप डीबी।

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