Add admin user to ruby on rails application - ruby-on-rails-3

I am new to ruby on rails
I am developing application which have four users Patient,Doctor,Nurse and Admin i have created patient user using devise now i want to create Admin user which will be used to do all administrative tasks like display all registered patients, etc..
so how can i add the new user to rails application. Do i need to create a new controllers for them
I tried to create the new user by referring this web page
http://rubyonrailshelp.wordpress.com/2014/01/03/creating-user-and-admin-model-using-devise-rails/
but it creates new files and update some existing files when i run this command
$rails generate devise:views
is it there feature or something went wrong i don't have an idea ?
please help

You have to create one devise user table with have one column as role_name:string which will save the your role name (Patient,Doctor,Nurse, Admin) and for admin it should be 'admin'...
And if its one in app then you will create by seed file (app/db/seed.rb)
As User.create(email: , password: , roll_name: 'admin' ) then
rake db:seed

Related

rails 5 using devise

hi currently i am making a new project using rails 5, and devise , my problem is that i am trying to get the current user that is logged into my webapp, and then get the current users id and save it into the database.
currently getting a lot of error and having trouble doing so.
my database are the following for the sample
Note this is the default devise setup for the user
User
username
password
email address
blog
title.text
body.text
user_id.integer
now my problem is that how do i get the current user that is logged into my webapp, and then save it into the blog database that contains the user_id. i havent added the blog yet into one of my samples , but i am currently failing at the results that i need to be making and pushing forward into it.
is there a way to scaffold it , or do i need to go commando and hardcode it, if so how is it possible to do so?
get the current user so that it gets the current user id and save it to each blog.
2.how do i push it into the controller
how do i do it into the model as well
i am a bit confused as to how it should be made and done , with rails any help will be appreciated
In your user.rb model
define relationship with user in this way
has_many :blogs
in your bolg.rb model
belongs_to :user
now in your create method in your blog controller
#user = current_user
then
if #user.blogs.create(blog_params)
#your logic
end
This automatically save user_id to database.

Changing default table for authentication Laravel 4

I am developing a package for Laravel 4 I need to add some authentication and authorization to this package, I need to use a table other than users table in this case: "admins"
The laravel application by default is looking for the table Users and check if the username and password exist there, I need my app to take advantage of default Authentication of Laravel (which looks the users table), but I also need to keep away the data of admins from the normal users and have authentication for another table as well (in 2 different tables).
Question:
How should I solve this issue? do I have to override the settings of auth.php file or can I extend this authentication in my own package so it work alongside with auth of Laravel 4?
To change that open app/config/auth.php
change 'table' => 'users' into 'table' => 'admins'
and also change 'model' => 'Admin'

How to set parent id when using nested attributes form?

I am building a Rails 3.2 webapp. In this app I am using the gem Act_as_tenant.
I also got accounts (tenants) that has many users (trough a join table).
I am creating both the account and the first user using nested attributes.
The user model got an attribute called account_id which I need to fill with the created account. Both the account and the user is created successfully BUT the id of the newly created account is not set in the users model (account_id). How can I make sure it is?

Add Spree to rails app with devise

I'm new at RoR but I'm loving every bit of it :)
I have a small app that uses devise for authentication and it's working fine.
Now I want to add a ecommerce part nad I decided for spree. I installed spree and during installation it asked me if I wanted to use the default authentication, I said 'no'. Then it asked me for the user model which I stated 'User'.
Now I enter my app, I login it goes to the products page on spree. That's ok, but when I try to access the admin part, I get redirected to the products page.
My doubts are:
- Should I install anyway the spree_auth_devise gem?
- Should this be a problem with the database and the users role? Because on the "spree_users" table I don't have anything, only on the Users table...
How can I associate one of my existing users to an admin user on spree?
Should this be the problem or I'm missing something else?
Did you run this:
rails g spree:custom_user User
http://guides.spreecommerce.com/authentication.html in the Initial Setup block
To check is user is admin:
user = User.find_by_email("master#example.com")
user.spree_roles << Spree::Role.find_or_create_by_name("admin")
## To test that this has worked, use the has_spree_role? method, like this:
user.has_spree_role?("admin")

User.admin with Devise ( Rails 3)

This is probably a simple question, but as I have never gone through the process thought I would seek assistance. I am using devise for my user authentication in my rails 3 app. Rather than create a seperate model for my 1 admin user i would like to set a user with admin => TRUE.
I have my User DB setup ready to go ( have added Admin column in the USER DB).
My question is how do i create a User so that the admin flag is set to TRUE. Would this be done in the console, if so what would the command look like?
All help appreciated
Check out section Listing 9.41. of Michael Hartl's tutorial.
User.toggle!(:admin)
or as I prefer:
u = User.find(1)
u.toggle!(:admin)