views:

278

answers:

2

Hi 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 UsersController < 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
+1  A: 

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.

Senthil
Thanks, I edited my post with the code. The nify_authentication looks like a great gem I would like to use in the future, but I think it just does what I already have running. I can sign up a user and log them in just fine. However what I am trying to do is sign up ANOTHER user when one user is logged in. This is failing to create that second user because it just wants to update the one currently logged in.
looloobs
I don't really see the point of letting users register others. It is too complicated for very little gain. One thing I can think of is creating invitations that users can send out to other users. This method doesn't let current user pick username and pass, but I imagine people don't like username and pass already choosen for them.
Senthil
Yes I have thought of the invitation route as well, I def have no intention of letting anyone else pick user name or password, that is done in the activation part. I need to have the ability for a person who is a user to put in other users into their specified group and by position, those criteria must be put in by that existing user. I realize that I probably need to looks into roles as well, but that doesn't seem to solve my problem of adding another user while one is signed in. Thanks.
looloobs
I think you're looking at it the wrong way. I guess you want an user to pick an user and put him/her in a certain group? Simple create a groups controller and let users add/delete members from that group. You don't need to be anywhere near users/sessions controller for that.
Senthil
A: 

Hi, 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.

Ole Morten Amundsen