Michael Hartl Rails教程第10章中的错误

时间:2015-01-11 14:44:55

标签: ruby-on-rails

我正在尝试让测试用于注册和帐户激活。看起来模板中的错误是尝试使用方法创建路径。这是错误:

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

最后,帐户激活控制器确实具有编辑功能,因此应由应用程序找到路径。在书中,编辑功能尚未实现,它仍然可以工作......我继续执行编辑功能:

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

5 个答案:

答案 0 :(得分:2)

我刚刚遇到了教程中遇到的同样问题。我发布这个问题,其他任何人都遇到了这个问题。问题出在我没有创建activation_token的用户模型中。如果有人遇到问题,请尝试确保使用before_create:create_activation_digest调用在User模型中创建activation_token。

谢谢, 马特

答案 1 :(得分:1)

我遇到了同样的问题。发生在我身上的是我删除了自己:

def create_activation_digest
self.activation_token = User.new_token
self.activation_digest = User.digest(activation_token)
end

我并不完全记得为什么要这样做,而是加入自我。回来为我修好了。

答案 2 :(得分:0)

教程中没有错误,很难理解您的应用程序出了什么问题,但如果它有所帮助,请查看我的https://bitbucket.org/juliausanova/sample_app/src。 也许你会在比较后找到这个bug。 希望它会有所帮助

答案 3 :(得分:0)

我遇到类似问题的错误消息:

  

ActionView :: MissingTemplate(缺少模板account_activations /编辑,使用{#A BUNCH OF INFO}编辑应用程序

在〜/ app / controllers / account_activations_controller.rb中我忘记添加:

  

redirect_to user

结束时的

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

答案 4 :(得分:0)

稍后再学习本教程:D

我有一个非常类似的错误,来自@mtmcgurn的检查用户模型的评论很有帮助!

在更改authenticated?方法以用于记忆和激活时,我没有从每一行中删除“ remember _”!