在我的Rails 3应用程序中,我正在重定向以在注册过程中进行登录。要注册的步骤应该是:Rails 3 NoMethod错误 - 配置文件中的配置文件未定义方法#使用CanCan编辑
- 用户创建用户和简介
- 在保存用户,用户登录到应用程序,并重定向到配置文件#编辑(
/signup/join
) - 在保存配置文件,用户被重定向到配置文件#秀(
/profiles/:id
)
我重新导向至/login
步骤1后,和我看到重定向后302错误。如果我注释掉我在profiles_controller.rb
before_filter :authenticate
和重做上面,我不重定向出/signup/join
步骤,但我得到以下错误:
NoMethodError in ProfilesController#edit
undefined method `profile' for nil:NilClass
我指了指我Profiles#edit
行动的第一行:
def edit
@profile = user.profile
if @profile.higher_ed?
higher_ed = HigherEd.find_or_create_by_name(:name => @profile.higher_ed)
end
if @profile.employer?
employer = Employer.find_or_create_by_name(:name => @profile.employer)
end
render :layout => "join_form"
end
我一直试图在我的应用程序中实现CanCan,所以我认为这是原因。但是,我注释了我的整个ability.rb
文件,问题依然存在。我很想弄清楚如何解决这个问题,而不用注释掉before_filter
。所以如果有人有想法,我会非常感激。因为我负责的康康舞这取决于current_user
,我会在我的application_controller.rb
与current_user
的定义开始:
protected
# Returns the currently logged in user or nil if there isn't one
def current_user
return unless session[:user_id]
@current_user ||= User.find_by_id(session[:user_id])
@current_user ||= User.find_by_auth_token!(cookies[:auth_token]) if cookies[:auth_token]
end
# Make current_user available in templates as a helper
helper_method :current_user
这里是我的users_controller.rb
:
class UsersController < ApplicationController
before_filter :authenticate, :only => [:edit, :update, :index]
layout "application"
def new
@user = User.new
@user.profile = Profile.new
if logged_in?
redirect_to current_user.profile
end
end
def create
@user = User.new(params[:user])
if @user.save
session[:user_id] = @user.id
redirect_to join_path, :notice => 'User successfully added.'
UserMailer.registration_confirmation(@user).deliver
else
render :action => 'new'
end
end
我profiles_controller.rb
:
class ProfilesController < ApplicationController
#before_filter :authenticate, :only => [:edit, :update]
helper_method :find_or_create_group
layout "application", :except => [:edit, :show]
def new
@profile = Profile.new(params[:profile])
end
def create
@profile = Profile.new(params[:profile])
if @profile.save
redirect_to @user.profile, :notice => 'User successfully added.'
else
render :new
end
if @profile.higher_ed?
HigherEd.find_or_create_by_name(:name => @profile.higher_ed)
end
if @profile.employer?
Employer.find_or_create_by_name(:name => @profile.employer)
end
if @profile.job_title?
JobTitle.find_or_create_by_name(:name => @profile.job_title)
end
if @profile.high_school?
HighSchool.find_or_create_by_name(:name => @profile.high_school)
end
end
def user
@user = current_user
end
def edit
@profile = user.profile
if @profile.higher_ed?
higher_ed = HigherEd.find_or_create_by_name(:name => @profile.higher_ed)
end
if @profile.employer?
employer = Employer.find_or_create_by_name(:name => @profile.employer)
end
render :layout => "join_form"
end
我的sessions_controller.rb
:
class SessionsController < ApplicationController
def new
end
def create
if user = User.authenticate(params[:email].downcase, params[:password])
session[:user_id] = user.id
cookies.permanent[:auth_token] = user.auth_token
if user.profile.higher_ed?
redirect_to user.profile, :notice => "Logged in successfully"
else
redirect_to join_path, :notice => "Logged in successfully"
end
else
flash.now[:alert] = "Invalid login/password. Try again!"
render :action => 'new'
end
end
def destroy
reset_session
cookies.delete(:auth_token)
redirect_to root_path, :notice => "You successfully logged out"
end
end
我ability.rb
的康康舞:
class Ability
include CanCan::Ability
def initialize(user)
user ||= User.new guest user
if user.role? :admin
can :manage, :all
else
can :manage, :all
end
end
end
我routes.rb
:
match "/signup/join" => "profiles#edit", :as => 'join'
我试图将其更改为current_user,但我认为current_user是问题。一旦我重新开始工作。 – tvalent2 2012-02-24 04:52:24