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 Bhatkhande <[email protected]>
> 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?
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---