On Feb 8, 2011, at 10:48 PM, Ben Hughes wrote:
Wow, thanks for the fast response.
> I think this has something to do with you overriding that "self.create"
> method on UserSession. If you have to override it, try invoking super(user)
> within it so as to make sure the actual authlogic create method gets run into
> Authlogic::Session::Base.
Renaming the method has no effect. As I’m doing new; save within it, I think
it’s just fine.
> Regardless of the above, I think this is definitely a problem:
>
> current_user = @user_session.record
> current_user.save!
>
> Be aware that the way this is written, you are simply declaring a local
> variable - not actually setting "current_user" as on the controller instance.
> To do that you need to explicitly use self.current_user:
>
> self.current_user = @user_session.record
> self.current_user.save!
>
> That being said, I don't know authlogic well enough to know if you can just
> set self.current_user and expect things to work, which is why you may need to
> call super(user) within that overridden create method.
No, I don’t need the current user here. I just want to save the organization
the current user is logged in with on that user. This doesn’t really have
anything to do with the case. A more concise rendering of what I’m doing would
be:
class UserSessionsController < ApplicationController
def create
@user = User.where(login: params['user_session']['login']).first
@user_session = UserSession.create params[:user_session], @user,
params[:user_session][:organization_id]
if @user_session.errors.empty?
redirect_back_or_default user_info_path
else
render action: :new
end
end
end
class UserSession < Authlogic::Session::Base
def self.create params, user, org_id
result = new params
result.user = user
result.org_id = org_id.to_i
result
end
end
class ApplicationController < ActionController::Base
before_filter :require_user
def current_user_session
return @current_user_session if defined?(@current_user_session)
@current_user_session = UserSession.find
end
def current_user
return @current_user if defined?(@current_user)
@current_user = current_user_session && current_user_session.record
end
def require_user
unless current_user
store_location
flash[:notice] = "You must be logged in to access this page"
redirect_to new_user_session_url
return false
end
end
end
--
SD Ruby mailing list
[email protected]
http://groups.google.com/group/sdruby