2016-02-27 105 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])' –