【发布时间】:2013-10-29 09:37:38
【问题描述】:
我开始使用 Rails 已经有几天了。我正在尝试制作一个要求用户在每种情况下都登录的表单应用程序。
所以我让用户登录 Railcast :http://railscasts.com/episodes/250-authentication-from-scratch
现在,我需要在我的其他控制器中进行所需的登录,因此用户在未登录的情况下无法访问整个应用程序。 我试过这个方法:
application_controller.rb
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
helper_method :current_user
private
def current_user
@current_user ||= User.find(session[:user_id]) if session[:user_id]
end
def logged_in
return true if current_user
end
def login_required
if logged_in false
redirect_to log_in_path and return false
end
end
end
categories_controller.rb
class CategoriesController < ApplicationController
before_filter :login_required
def new
def index
@categories = Categorie.all
end
它返回给我这个错误:
CategoriesController#index 中的参数错误 参数数量错误(1 代表 0)
Extracted source (around line #14):
def logged_in
return true if current_user
end
我的 before_filter :login_required 需要别的东西吗? 我不太明白这个错误。
【问题讨论】:
-
你能显示 CategoriesController, index 方法吗?
-
我在编辑我的帖子时添加了它:)
标签: ruby-on-rails before-filter