我正在嘗試使註冊和帳戶激活工作。它看起來像在試圖使用方法創建路線的模板中有錯誤。以下是錯誤:Michael Hartl Rails教程中的錯誤第10章
ERROR["test_valid_signup_information_with_account_activation", UsersSignupTest, 1.614055]
test_valid_signup_information_with_account_activation#UsersSignupTest (1.61s)
ActionView::Template::Error: ActionView::Template::Error: No route matches {:action=>"edit", :controller=>"account_activations", :email=>"[email protected]", :format=>nil, :id=>nil} missing required keys: [:id]
的模板,創建路線:
<%= link_to "Activate", edit_account_activation_url(@user.activation_token,
email: @user.email) %>
我在路由使用的資源,所以你會覺得這會產生正確的路線。這裏是我的路線文件:
Rails.application.routes.draw do
root 'static_pages#home'
get 'help' => 'static_pages#help'
get 'about' => 'static_pages#about'
get 'contact' => 'static_pages#contact'
get 'signup' => 'users#new'
get 'login' => 'sessions#new'
post 'login' => 'sessions#create'
delete 'logout' => 'sessions#destroy'
resources :users
resources :account_activations, only: [:edit]
end
錯誤說,它缺少一個id,這對我意味着它需要被插入到訪問路線之前數據庫(以及由此產生的ID)的記錄。但上線後保存@ user.send_activation_email
class UsersController < ApplicationController
before_action :logged_in_user, only: [:index, :edit, :update, :destroy]
before_action :correct_user, only: [:edit, :update]
before_action :admin_user, only: :destroy
def create
@user = User.new(user_params)
if @user.save
@user.send_activation_email
flash[:info] = "Please check your email to activate your account."
redirect_to root_url
else
render 'new'
end
end
隨着用戶類中定義的方法send_activation_email發生錯誤:
class User < ActiveRecord::Base
attr_accessor :remember_token, :activation_token
before_save :downcase_email
before_create :create_activation_digest
# Sends activation email.
def send_activation_email
UserMailer.account_activation(self).deliver_now
end
最後,賬戶激活控制器確實有一個編輯功能,所以路線應該被應用找到。在這本書中,編輯功能尚未實現,但還是想上班......我說幹就幹,反正實現編輯功能:
class AccountActivationsController < ApplicationController
def edit
user = User.find_by(email: params[:email])
if user && !user.activated? && user.authenticated?(:activation, params[:id])
user.update_attribute(:activated, true)
user.update_attribute(:activated_at, Time.zone.now)
log_in user
flash[:success] = "Account activated!"
redirect_to user
else
flash[:danger] = "Invalid activation link"
redirect_to root_url
end
end
end
所以我想的問題是,爲什麼不這個ID生成?
最後,產生錯誤的測試:
test "valid signup information" do
get signup_path
assert_difference 'User.count', 1 do
post_via_redirect users_path, user: { name: "Example User",
email: "[email protected]",
password: "password",
password_confirmation: "password" }
end
# assert_template 'users/show'
# assert is_logged_in?
end
結束聽起來像activation_token爲零。 –
我有同樣的問題,你能解決嗎?另外在我的情況下,edit_account_activation [s] _url正在工作,注意到s,沒有編譯器說沒有這樣的方法 –