2015-01-11 105 views
2

我正在嘗試使註冊和帳戶激活工作。它看起來像在試圖使用方法創建路線的模板中有錯誤。以下是錯誤: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 
+0

結束聽起來像activation_token爲零。 –

+0

我有同樣的問題,你能解決嗎?另外在我的情況下,edit_account_activation [s] _url正在工作,注意到s,沒有編譯器說沒有這樣的方法 –

回答

2

我剛剛遇到了相同的問題,通過教程。我發佈這個incase任何人遇到問題。問題出在我沒有創建activation_token的用戶模型中。如果有人遇到問題,請嘗試確保您正在使用before_create:create_activation_digest調用在User模型中創建activation_token。

謝謝, 馬特

1

我遇到了同樣的問題。我發生了什麼事是我刪除了自我:

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

我不記得爲什麼我這樣做,但增加了自我。回到固定它爲我。

0

我有類似的問題與一錯誤信息:

ActionView::MissingTemplate (Missing template account_activations/edit, application edit with {#A BUNCH OF INFO}

在〜/應用/控制器/ 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

相關問題