我正在尝试使注册和帐户激活工作。它看起来像在试图使用方法创建路线的模板中有错误。以下是错误: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,没有编译器说没有这样的方法 –