簡體   English   中英

Michael Hartl Rails教程第10章中的錯誤

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM