# Display signup form to enter admin account details

**URL:** <https://www.ruby-forum.com/t/display-signup-form-to-enter-admin-account-details/156253>\
**Category:** Rails\
**Created:** [December 23, 2008, 2:21pm UTC](https://www.ruby-forum.com/t/display-signup-form-to-enter-admin-account-details/156253 "2008-12-23T14:21:49Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![amita0204](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e56c9b/32.png) [@amita0204](https://www.ruby-forum.com/u/amita0204)\
**Post date:** [December 23, 2008, 2:21pm UTC](https://www.ruby-forum.com/t/display-signup-form-to-enter-admin-account-details/156253/1 "2008-12-23T14:21:49Z")

</div>

I would like to display a user account signup form if no user is  
present. The signup form will then create a new user account with admin  
privileges.

Right now I am creating a pre-defined / hard coded admin account.  
Whenever user clicks on a login page, the application checks for an  
account with admin privileges. If no such account is found, then I am  
creating an admin account. Usually it will occur during first run of the  
application.

I would like to prompt user to create an admin account automatically,  
i.e., without user having to click on any link. How can I invoke my  
signup method in this manner? Where should I define it and call it from?

Thanks,  
Amita.

---

<div class="post-metadata">

**Author:** ![amita0204](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e56c9b/32.png) [@amita0204](https://www.ruby-forum.com/u/amita0204)\
**Post date:** [December 23, 2008, 2:28pm UTC](https://www.ruby-forum.com/t/display-signup-form-to-enter-admin-account-details/156253/2 "2008-12-23T14:28:45Z")

</div>

first of all: what sense makes an admin-account if everybody gets one  
automatically?  
second: what information do you store? without having the user at  
least choose a username, password, how would you recognise which user  
is which?

i’d really want to help, but it seems i don’t understand what you are  
doing here.

---

<div class="post-metadata">

**Author:** ![amita0204](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e56c9b/32.png) [@amita0204](https://www.ruby-forum.com/u/amita0204)\
**Post date:** [December 23, 2008, 3:43pm UTC](https://www.ruby-forum.com/t/display-signup-form-to-enter-admin-account-details/156253/3 "2008-12-23T15:43:20Z")

</div>

On 23 Dez., 15:31, Amita B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> What I would like to do:  
> When user runs the application and no admin account exists, then display  
> a signup/create admin form to create an admin account.  
> if i understand correctly, you need to write some code roughly like  
> the following in your controller:

if User.find(:all, :conditions =\> {:group\_id =\> Group.find\_by\_name  
(“admin”).id}).size == 0

# render create\_admin\_account as none exists so far

else

# admin-account exists =\> just login

end

> Problem: Every time user clicks on the login button, the method to make  
> admin account if no exists is called upon.  
> do you already have some code to see where what could be wrong about  
> it?

---

<div class="post-metadata">

**Author:** ![amita0204](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e56c9b/32.png) [@amita0204](https://www.ruby-forum.com/u/amita0204)\
**Post date:** [December 23, 2008, 3:31pm UTC](https://www.ruby-forum.com/t/display-signup-form-to-enter-admin-account-details/156253/4 "2008-12-23T15:31:10Z")

</div>

Not everyone gets an admin account automatically.  
Current approach:  
The user already knows username and password of this admin account  
(included in README file). When user clicks on the login button, the  
application checks if any user with admin privileges exists. If not,  
then an account with admin privileges is setup. Admin user can change  
his/her password thereafter. On subsequent runs (when admin account is  
already exists), another admin account won’t be created and there exists  
only one admin account. Also, once created the admin account can not be  
deleted.

Problem: Every time user clicks on the login button, the method to make  
admin account if no exists is called upon.

What I would like to do:  
When user runs the application and no admin account exists, then display  
a signup/create admin form to create an admin account. Usually this will  
occur during first run, but not necessarily.

How can I implement this? Other suggestions are welcome…

Thanks,  
Amita.

MaD wrote:

> first of all: what sense makes an admin-account if everybody gets one  
> automatically?  
> second: what information do you store? without having the user at  
> least choose a username, password, how would you recognise which user  
> is which?
> 
> i’d really want to help, but it seems i don’t understand what you are  
> doing here.

---

<div class="post-metadata">

**Author:** ![amita0204](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e56c9b/32.png) [@amita0204](https://www.ruby-forum.com/u/amita0204)\
**Post date:** [December 24, 2008, 9:30am UTC](https://www.ruby-forum.com/t/display-signup-form-to-enter-admin-account-details/156253/5 "2008-12-24T09:30:11Z")

</div>

your problem is that you are calling

User.make\_admin\_if\_none

and in there you are asking

self.find\_by\_role(admin)

now, as this is a class-method self would be the user-class, but not  
an instance of it, which means it is not a single user and thus cannot  
have the role ‘admin’. therefor your if-clause always gets evaluated  
to false and you create a new user every time someone hits that  
button.

make it an instance-method (= get rid of the ‘self.’ in method-  
definition and adjust the logic accordingly). and call it like this

user = User.authenticate(params[:username], params[:password])  
user.make\_admin\_if\_none

hope this helps…

---

<div class="post-metadata">

**Author:** ![amita0204](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e56c9b/32.png) [@amita0204](https://www.ruby-forum.com/u/amita0204)\
**Post date:** [December 23, 2008, 4:17pm UTC](https://www.ruby-forum.com/t/display-signup-form-to-enter-admin-account-details/156253/6 "2008-12-23T16:17:04Z")

</div>

I think our logic is pretty much similar. The problem is where should I  
place my code in the new approach. In current scenario, login  
button/action is triggering the make\_admin\_if\_none method. So for every  
click on login button this method will be called upon. So is there any  
better way to do this? See code below…

Code:

# login controller

# User clicks on login button, which invokes login action

def login  
if request.post?  
User.make\_admin\_if\_none #make sure admin exists  
user = User.authenticate(params[:username], params[:password])  
params[:password] = nil  
if user  
session[:user] = user.id  
session[:user\_role] = !user.role.nil? ? user.role.name : nil  
uri = session[:original\_uri]  
session[:original\_uri] = nil  
redirect\_to(uri || {:controller =\> :main, :action =\> :index})  
return  
end  
flash[:notice] = FAILED\_LOGIN  
end

# User model

# User.make\_admin\_if\_none

# This will create admin account if none

def self.make\_admin\_if\_none  
return if self.find\_by\_role(“admin”)

```
user = User.new(:username => "admin", :password => "admin")
role = Role.find_by_name('admin')
user.role = role
user.save!

```

end

# Roles table is already populated with roles

Thanks,  
Amita.

MaD wrote:

> On 23 Dez., 15:31, Amita B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > What I would like to do:  
> > When user runs the application and no admin account exists, then display  
> > a signup/create admin form to create an admin account.  
> > if i understand correctly, you need to write some code roughly like  
> > the following in your controller:
> 
> if User.find(:all, :conditions =\> {:group\_id =\> Group.find\_by\_name  
> (“admin”).id}).size == 0
> 
> # render create\_admin\_account as none exists so far
> 
> else
> 
> # admin-account exists =\> just login
> 
> end
> 
> > Problem: Every time user clicks on the login button, the method to make  
> > admin account if no exists is called upon.  
> > do you already have some code to see where what could be wrong about  
> > it?

---

<div class="post-metadata">

**Author:** ![amita0204](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e56c9b/32.png) [@amita0204](https://www.ruby-forum.com/u/amita0204)\
**Post date:** [December 24, 2008, 9:46am UTC](https://www.ruby-forum.com/t/display-signup-form-to-enter-admin-account-details/156253/7 "2008-12-24T09:46:00Z")

</div>

another thought:  
it would be better to include that kind of logic into the registration-  
process. that way you dont have to call it every time someone logs in.

something like this:

def register  
user = User.new  
admins = Group.find\_by\_name(“admin”)  
if admins.users.size == 0  
user.groups \<\< admins  
end  
end

hard to tell you which way to go. in the end it’s YOUR application.  
still i hope i could give you some inspiration.
