Rails不断渲染错误页面

时间:2016-02-27 01:27:50

标签: ruby-on-rails

我是rails的新手,我按照一些教程创建了一个登录系统。一切正常,直到我修改了一些东西(不知道是什么),现在rails继续将我重定向到404页面。

这就是我在控制台中的内容:

    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 / admin

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

routes.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

1 个答案:

答案 0 :(得分:0)

删除您的Cookie。您正在尝试从会话中加载陈旧数据:

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

您正在加载ID为1的用户(基于日志中的输出),该用户不再存在。您可能已删除并重新创建数据库或重新运行迁移或任何可能破坏您的开发数据的事物。

用于current_user的代码在某种程度上是天真的,正是出于这个原因。您应该处理未找到用户的情况,并销毁会话而不是呈现404.您也不应该使用@user变量;您最终可能会在Users控制器中覆盖同名变量。

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