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? Any suggestions?
ruby version: 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 '
1 runs, 0 assertions, 0 failures, 1 errors, 0 skips
user_mailer/account_activation.html.erb:
Sample App
<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
and the 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
Aucun commentaire:
Enregistrer un commentaire