10.2关于Michael Hartl的Rails教程的错误

时间:2015-05-23 03:22:07

标签: ruby-on-rails testing mailer

我努力寻找答案,但我不确定问题是什么。我想知道最近是否有更新改变了某些内容的语法?或许我错过了Rails教程中的一步?有什么建议?

Rails版本:4.2.0

我在运行测试时遇到此错误:

bundle exec rake test:mailers

1) Error:
UserMailerTest#test_account_activation:
ActionView::Template::Error: undefined method `edit_account_activation_url' for #<#<Class:0x00000008ace808>:0x00000008ac9240>
app/views/user_mailer/account_activation.html.erb:9:in `_app_views_user_mailer_account_activation_html_erb__2122098350412258357_72816220'
app/mailers/user_mailer.rb:11:in `account_activation'
test/mailers/user_mailer_test.rb:9:in `block in <class:UserMailerTest>'

1 runs, 0 assertions, 0 failures, 1 errors, 0 skips

示例应用

user_mailer/account_activation.html.erb

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

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

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

routes.rb

Rails.application.routes.draw do

  get 'sessions/new'
  resources :users
  resources :acccount_activations, only: [:edit]


  #Static pages
  root 'static_pages#home'
  get 'help' => 'static_pages#help'
  get 'about' => 'static_pages#about'
  get 'contact' => 'static_pages#contact'
  get 'signup' => 'users#new'


  #get 'users/new'

  #Login/Logout actions
  get    'login'   => 'sessions#new'
  post   'login'   => 'sessions#create'
  delete 'logout'  => 'sessions#destroy'

end

user_mailer.rb

class UserMailer < ApplicationMailer
  default from: 'noreply@example.com'

  # 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

  # Subject can be set in your I18n file at config/locales/en.yml
  # with the following lookup:
  #
  #   en.user_mailer.password_reset.subject
  #
  def password_reset
    @user = user
    @greeting = "Hi"

    mail to: "to@example.org"
  end

end

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+\-.]+@[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 for session
    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

    #Activates an account.
    def activate
        update_attribute(:activated, true)
        update_attribute(:activated_at, Time.zone.now)
    end

    #Sends activation email.
    def send_activation_email
        UserMailer.account_activation(self).deliver.now
    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

user_mailer_test.rb

require 'test_helper'

class UserMailerTest < ActionMailer::TestCase

  test "account_activation" do
    user = users(:michael)
    user.activation_token = User.new_token
    mail = UserMailer.account_activation(user)
    assert_equal "Account activation", mail.subject
    assert_equal [user.email], mail.to
    assert_equal ["noreply@example.com"], mail.from
    assert_match user.name,               mail.body.encoded
    assert_match user.activation_token,   mail.body.encoded
    assert_match CGI::escape(user.email), mail.body.encoded
  end

  #test "password_reset" do
    #mail = UserMailer.password_reset
    #assert_equal "Password reset", mail.subject
    #assert_equal ["to@example.org"], mail.to
    #assert_equal ["from@example.com"], mail.from
    #assert_match "Hi", mail.body.encoded
  #end

end

1 个答案:

答案 0 :(得分:1)

棘手的追踪但是你的路线只是一个简单的拼写错误:)

routes.rb中:

更改此行:

TextBox2.Text = ""

以下内容:

resources :acccount_activations, only: [:edit] 

这是一个微妙的区别,但您的资源名称中还有一个额外的 c

希望这有帮助!