![](/img/trans.png)
[英]Michael Hartl's Rails Tutorial Chapter 10 destroy method
[英]Error in Michael Hartl Rails Tutorial Chapter 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=>"user@example.com", :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
最后,帐户激活控制器确实具有编辑功能,因此该路线应由应用找到。 在这本书中,尚未实现edit功能,并且仍然可以正常工作...我还是继续实现了edit功能:
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: "user@example.com",
password: "password",
password_confirmation: "password" }
end
# assert_template 'users/show'
# assert is_logged_in?
end
在本教程中,我遇到了同样的问题。 如果有人遇到问题,我会发布此信息。 问题出在我没有创建activation_token的用户模型中。 如果有人遇到问题,请尝试确保您使用before_create:create_activation_digest调用在用户模型中创建了激活令牌。
谢谢,马特
我遇到了同样的问题。 我发生的事情是我从以下位置删除了自己:
def create_activation_digest
self.activation_token = User.new_token
self.activation_digest = User.digest(activation_token)
end
我不完全记得为什么要这么做,而是添加自我。 重新为我修复它。
教程中没有错误,并且很难理解您的应用程序有什么问题,但是如果有帮助,请查看我的https://bitbucket.org/juliausanova/sample_app/src 。 也许您会在比较后发现错误。 希望对你有帮助
我也遇到类似的问题,并显示以下错误消息:
ActionView :: MissingTemplate(缺少模板account_activations / edit,使用{#A BUNCH OF INFO}编辑应用程序
在〜/ app / controllers / account_activations_controller.rb中,我忘了添加:
redirect_to用户
在......的最后
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, Time.zone.now) log_in user flash[:success] = "Account activated!" redirect_to user
稍后再阅读本教程:D
我有一个非常类似的错误,来自@mtmcgurn的用于检查用户模型的评论有所帮助!
何时更改已authenticated?
用于记忆和激活的方法我没有从每一行中删除“ remember_”!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.