[英]10.2 error on Michael Hartl's Rails Tutorial
I tried hard to find an answer, but I'm not sure what the problem is. 我努力寻找答案,但是我不确定是什么问题。 I'm wondering if there was a recent update that changed the syntax of something?
我想知道最近是否有更改某些语法的更新? Or perhaps I missed a step in the Rails tutorial?
还是我错过了Rails教程中的步骤? Any suggestions?
有什么建议么?
Rails version: 4.2.0 Rails版本:4.2.0
I keep getting this error when I run the test: 运行测试时,我不断收到此错误:
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
: 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
Tricky one to track down but it was just a simple misspelling in your routes :) 棘手的人来追踪,但这只是您路线中的一个简单拼写错误:)
routes.rb: routes.rb中:
Change this line: 更改此行:
resources :acccount_activations, only: [:edit]
To the following: 要以下内容:
resources :account_activations, only: [:edit]
It's a subtle difference, but there was an extra c in your resource name. 这是一个细微的差别,但是您的资源名称中有一个额外的c 。
Hope this helps! 希望这可以帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.