开发者

Authlogic Help! Registering a new user when currently logged-in as a user not working

开发者 https://www.devze.com 2022-12-19 00:45 出处:网络
Just as a disclaimer I am new to rails and programming in general so apologize for misunderstanding something obvious.

Just as a disclaimer I am new to rails and programming in general so apologize for misunderstanding something obvious.

I have Authlogic with activation up and running. So for my site I would like my users who are logged in to be able to register other users. The new user would pick their login and password through the activation email, but the existing user needs to put them in by email, position and a couple other attributes. I want that to be done by the existing user.

The problem I am running into, if I am logged in and then try and create a new user it just tries to update the existing user and doesn't create a new one. I am not sure if there is some way to fix this by having another session start??? If that is even right/possible I wouldn't know how to go about implementing it.

I realize without knowing fully about my application it may be difficult to answer this, but does this even sound like the right way to go about this? Am I missing something here?

Users Controller:

class UsersCo开发者_如何学运维ntroller < ApplicationController
before_filter :require_no_user, :only => [:new, :create]
before_filter :require_user, :only => [:show, :edit, :update]

def new
  @user = User.new
end

def create
   @user = User.new

 if @user.signup!(params)
   @user.deliver_activation_instructions!
   flash[:notice] = "Your account has been created. Please check your e-mail for your account activation instructions!"
   redirect_to profile_url
 else
   render :action => :new
 end

end


def show
  @user = @current_user
end

def edit
 @user = @current_user
end

def update
  @user = @current_user # makes our views "cleaner" and more consistent
  if @user.update_attributes(params[:user])
    flash[:notice] = "Account updated!"
    redirect_to profile_url
   else
   render :action => :edit
   end
 end
end

My User_Session Controller:

 class UserSessionsController < ApplicationController
   before_filter :require_no_user, :only => [:new, :create]
   before_filter :require_user, :only => :destroy

def new
  @user_session = UserSession.new
end

def create
  @user_session = UserSession.new(params[:user_session])
    if @user_session.save
    flash[:notice] = "Login successful!"
      if @user_session.user.position == 'Battalion Commander' : redirect_to battalion_path(@user_session.user.battalion_id)
      else
      end
    else
  render :action => :new
  end
end

 def destroy
  current_user_session.destroy
  flash[:notice] = "Logout successful!"
  redirect_back_or_default new_user_session_url
 end
end


Could you paste your users and users_session controller code?

I suggest using Ryan Bates' nifty_authentication gem. You can use authologic instead of default restful_authentication with

script/generate nifty_authentication --authlogic

Works like a charm.


I've done this with no probs, but know how hard it can be to port yourself to a new language and many new libraries! Hang in there! :)

I think that it might be the before_filter :require_no_user on new and create that blocks you.

What do you mean with this? Does it render the edit view? Or is this a result of a post/put?

it just tries to update the existing user and doesn't create a new one.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号