How to Create a User Profile in Rails? - ruby-on-rails

I am using Devise for user registration and authentication but want to have user profiles too, for example /user/john-doe with all the fields like Company Name, Phone Number etc which the person can add.
Is there any Rails way to get it done out of the box? any gem for this? if not, can someone give me some direction on how to do it?

There is no gem to add company name, phone etc (at least I know :)), but normally what I do is,
I add the above columns to table, via a migration
Ex: add_column :users, :company_name, :string
then I update the model accordingly
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me, *:company_name*
end
then I run this command to copy the devise views
rails generate devise:views
then I update the corresponding view with new text_boxes etc..
HTH

Related

Adding extra validations for Devise validatable

So currently validatable validates the presence of email and password. It can also validate an emails format. However, my user model requires more than just an email and password. I am also requiring a first, last and user name. So in order for me to validate the presence of these attributes I have to use rails validates as shown:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
validates :first_name, presence: true
validates :last_name, presence: true
validates :user_name, presence: true
end
I was wondering if there was a way to add first, last and user name to the validatable action. I have checked the devise.rb file and found the validatable config for a password_length and email_regexp but don't quite know how I can add additional attributes to the validatable function. Obviously this isn't a huge deal, but would be nice for cleaning up code in my User's model. Thank you for any responses to my questions.
While you could potentially monkeypatch Devise::Models::Validatable at runtime it would be rather foolish. Its going to take 5 times more code and potentially break upgrades.
The whole point of the module is to provide models with the basic validations needed for Devise to work out of the box.
What you are adding are validations which are specific to your application. As such it belongs in your application - don't try to stuff it back into the library.
Instead can clean up your model by:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
validates_presence_of :first_name, :last_name, :user_name
end

How To Recover a Password with Devise ( Ruby on Rails)

I'm trying to recover a user's password with devise, but it generates the following error
undefined method `reset_password_sent_at=' for #<User:0x007fb78cfafb68>
Can anyone help me with this, since I'm new to Ruby on Rails?
What is the best way to recover a password and email the user using Devise? Thank you very much...
I'm use devise (2.2.3)
User.rb
require 'digest/md5'
class User < ActiveRecord::Base
# Setup accessible (or protected) attributes for your model
belongs_to :shop
before_create :compute_email_md5
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable,
:recoverable,
:rememberable,
:trackable,
:validatable,
:token_authenticatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email,
:email_md5,
:password,
:password_confirmation,
:shop_id,
:role,
:terms,
:name,
:notify_on_order_received
validates :terms, :acceptance => true, :on => :create
end
THE SOLUTION IS
add reset_password_sent_at column to user table
As you've discovered, passord recovery requires that the model have a reset_password_sent_at column. Adding it via migration should solve this problem.
As for the reason this is happening, I'm guessing you added password recovery (the :recoverable module) after initially generating your Devise-enabled model (User). That's why Devise's generator didn't create that column for you.

Get variable from another table in ROR models

I am trying to get a user who has not filled out their profile to redirect to the edit page.
I have two tables - Users (Devise handles) and Profiles.
Profiles has a row called profile_complete
In my user model I am trying to define a method called profile_complete which takes the boolean of profiles_complete and passes it back.
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me
has_many :profiles, :dependent => :destroy
def profile_complete?
self.profile.profile_complete == true
end
end
But cannot seem to figure out what the line in the profile_complete method is. I have got the other bits working in the correct place but cant get this variable across Any help? Cheers :)
def profile_complete?
self.attributes.values.include?(nil) ? false : true
end

Rails : Adding an admin role using devise who can see all the users

I need to create an admin role using devise for my app. I've created basic authentication using devise . I have a devise user model in my app but now i need an admin who can show edit and destroy all the users. I tried following the tutorials but none of them helped.
I am using rails 3.0.10 and ruby 1.9.2-p290.
You just define role.rb first by creating migratioin
rails g model role name:string
then in role.rb
class Role
has_one:user
end
And in user model
class user
belongs_to :role
end
Insert two roles into DB
1.admin
2.user
Then check by this
if user.role.name == "admin"
# can do all your logic
else
your logic
end
Make sure insert role_id:integer into user model
Try it.......
I have similar requirement as yours and also don't want any user to be able to signup. All that will be taken care by Admin. Her what I've done.
I added another devise model called Admin
rails generate devise MODEL
Disable 'Registerable' for User model so that user cannot singup by themself
user.rb
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :registerable, :timeoutable and :omniauthable
devise :database_authenticatable, :recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me, :first_name, :last_name, :role, :admin
# attr_accessible :title, :body
end
Enable CRUD for user by using sample from here: https://gist.github.com/1056194
Finally protect protect the users controller like so
users_controller.rb
# Add this
before_filter :authenticate_admin!
Hope this helps.

Devise - login from two model

I have two user models, first is from remote database as legacy and for internal company purposes. (Employee logins). Second is our project for public registration and sign in but I want one login form. I have searching long time, but some solutions are confusing for me.
First legacy looks like (only for reading and authentication):
class CrmUser < ActiveRecord::Base
require Rails.root.join('lib', 'devise', 'encryptors', 'sha1')
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable, :rememberable, and :omniauthable
establish_connection "crm_data"
set_table_name :users
devise :database_authenticatable, :encryptable, :authentication_keys => [:login]
alias_attribute :encrypted_password, :crypted_password
alias_attribute :password_salt, :salt
# Setup accessible (or protected) attributes for your model
attr_accessible :login, :password, :password_confirmation, :remember_me, :role_id, :first_name, :last_name
And second, for public and registration:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable, :rememberable, and :omniauthable
devise :database_authenticatable, :registerable, :authentication_keys => [:login]
alias_attribute :login, :email
# Setup accessible (or protected) attributes for your model
attr_accessible :login, :password, :password_confirmation, :remember_me, :role_id, :first_name, :last_name
Now I don't know how to do that User controller try to authentication from first model, and when user doesn't exists, go to second model and try it again.
Using:
Rails 3.1
Devise 1.4.3
EDIT:
In wiki of Devise is something about multiple model, but I'm little bit confused, there are not example more complex.
Thank you.
Regards, Rado
You should monkeypatch find_for_authentication method from devise/models/authenticatable.rb
module Devise
module Models
module Authenticatable
def find_for_authentication(conditions)
#put your authentication logic here
end
end
end
end
About authentication logic:
Using two models for authentication in your case it's realy bad idea. How do u want build relations with two users models? It's a lot of unnecessary code.
Correct way of resolving your problem is make some synchronization between yours tables.
Try to authenticate user with base User model.
If user credentials was wrong - try to authenticate him with CrmUser model.
If authentication with CrmUser was OK add him to users table if he doesn't exists there already.
Return User's model object.

Resources