2016-02-27 132 views
0

我是新手,我跟着一些教程來創建一個登錄系統。一切正常,直到我修改了一些東西(不知道是什麼),現在rails繼續將我重定向到404頁面。Rails保持渲染錯誤頁面

這是我在控制檯:

Started GET "/admin/users" for 127.0.0.1 at 2016-02-27 01:24:30 +0000 
    ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations" 
Processing by Admin::UsersController#index as HTML 
    User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 1]] 
    Rendered errors/error_404.html.haml within layouts/application (12.2ms) 
Completed 404 Not Found in 329ms (Views: 323.1ms | ActiveRecord: 0.4ms) 

,這是該指數:

<h1> User's index </h1> 

<table class="table table-bordered table-hover"> 
    <thead> 
    <tr> 
     <th>Email</th> 
     <th>Role</th> 
     <th>Created</th> 
     <th>Actions</th> 
    </tr> 
    </thead> 
    <tbody> 
    <% @users.each do |user| %> 
     <tr> 
     <td><%= user.email %></td> 
     <td><%= user.role %> </td> 
     <td><%= time_ago_in_words(user.created_at) %> ago</td> 
     <td> 
      <% if can? :update, @user %> 
      <%= link_to 'Edit', edit_admin_user_path(user) %> 
      <% end %> 
      <!-- <% if can? :delete, @user %> 
      <%= link_to "delete", admin_user_path(user), :method => :delete, :confirm => "You sure?", :title => "Delete #{task.name}" %> 
      <% end %> --> 
     </td> 
     </tr> 
    <% end %> 
    </tbody> 
</table> 

<p> <%= link_to 'New user', new_admin_user_path %> </p> 

你需要什麼其他的文件嗎?我真的不知道要發佈什麼。

這些都是在controlller /管理

application_controller.rb

class Admin::ApplicationController < ApplicationController 
    before_action :authorize 

    def current_user 
    @user ||= User.find(session[:current_user_id]) if session[:current_user_id] 
    end 

    def authorize 
    unless current_user 
     redirect_to '/login', alert: 'Please login' 
    end 
    end 
end 

users_controller.rb

class Admin::UsersController < Admin::ApplicationController 
    before_action :find_user_from_params, only: [:edit, :update, :destroy] 

    def index 
    @users = User.all 
    end 
.. 
end 

sessions_controller.rb

class Admin::SessionsController < Admin::ApplicationController 
    before_action :authorize, except: [:new, :create] 

    def new 
    end 

    def create 
    @user = User.find_by(email: params[:email]).try(:authenticate, params[:password]) 
    if @user 
     session[:current_user_id] = @user.id 
     redirect_to admin_users_url, notice: 'You have successfully signed in' 
    else 
     flash[:alert] = 'There was a problem with your username or password' 
     render :new 
    end 
    end 

    def destroy 
    session[:current_user_id] = nil 
    redirect_to '/login', notice: 'You have successfully logged out' 
    end 
end 

路線。 rb

Rails.application.routes.draw do 

    get '/login' => 'admin/sessions#new' 
    get '/logout' => 'admin/sessions#destroy' 


    match "/403", to: "errors#error_403", via: :all 
    match "/404", to: "errors#error_404", via: :all 
    match "/422", to: "errors#error_422", via: :all 
    match "/500", to: "errors#error_500", via: :all 

    get :ie_warning, to: 'errors#ie_warning' 
    get :javascript_warning, to: 'errors#javascript_warning' 

    root to: "pages#index" 

    namespace :admin do 
    resources :sessions, only: [:new, :create, :destroy] 
    resources :users 
    end 

end 
+0

這是用戶。我能夠看到/編輯/添加用戶,直到我修改'我不知道是什麼' –

+0

@ developer033這根本不是真的。沒有任何理由讓您的模型命名空間與您的控制器相同。路由和控制器命名空間與模型或數據庫無關。 – meagar

回答

0

刪除您的cookies。您正在嘗試從會話負載陳舊的數據:

@user ||= User.find(session[:current_user_id]) if session[:current_user_id] 

你加載用戶ID爲1(根據你的日誌輸出)不再存在。您可能已經刪除並重新創建了您的數據庫,或者重新運行遷移或任何可能破壞您的開發數據的事情。

正因爲這個原因,您用於current_user的代碼有點幼稚。您應該處理找不到用戶的情況,並銷燬會話而不是呈現404。您也不應該使用@user變量;您可能會在用戶控制器中覆蓋相同名稱的變量。

def current_user 
    begin 
    @current_user ||= User.find(session[:current_user_id]) if session[:current_user_id] 
    rescue ActiveRecord::RecordNotFound 
    session.destroy 
    redirect_to login_path 
    end 
end 
+0

謝謝。這解決了問題。只是編輯到'開始' –

+0

我仍然得到錯誤不幸.. –

+0

顯然'find_by_id'導致我認爲....。我檢查了一個會話[:current_user_id]返回4,但這一行給出錯誤'User.find_by_id(session [:current_user_id])' –