I'm trying to separate models from an RoR app into a gem. I'm getting an error when I extended the User model from the gem for adding Devise instance methods
I've tried different methods
User.class_eval and ModelsGem::User.class_eval
Single table inheritance like: class SuperClass < ModelsGem::User; end
Overriding the model class like class User < ActiveModel::Base
None of them worked with devise.. However, I could access methods of User model from the gem in the app and everything is working as expected other than devise.
You can do something like this
rails g devise:views
rails g devise user
if we wanna to add sth like first name and last name put it in the db before rake db:migrate
t.string :first_name
t.string :last_name
then rake db:migrate
in the user model
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable, :confirmable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :confirmable
end
I hope I did solve your problem
Related
I want to add a timeoutable model to devise after my initial configuration.
I enabled the config/initializers/devise.rb:
config.timeout_in = 30.minute
But where do I actually add the devise :timeoutable model?
After you install devise you need to configure model using built in generator
rails generate devise MODEL
example rails generate devise User. See this section
https://github.com/plataformatec/devise#user-content-getting-started
Once you have that inside app/models/user.rb you can add desired modules
class User < ApplicationRecord
devise :timeoutable, :database_authenticatable, :registerable,:recoverable, :rememberable, :trackable, :validatable, :omniauthable, :confirmable
end
Hope it helps
I'm trying to figure out how to create a new profile for the user that has just been created,
I'm using devise on the User model, and the User model has a one to one relationship with the UserProfile model.
Here's what my User Model looks like:
class User < ActiveRecord::Base
has_and_belongs_to_many :roles
belongs_to :batch
has_one :user_profile
after_create :create_user_profile
def create_user_profile
self.user_profile.new(:name => 'username')
end
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
end
This generate the following error:
undefined method `new' for nil:NilClass
i've tried User.find(1).user_profile in rails c and that works so I'm pritty sure the relationship is setup correctly,
I'm probably being a big fat noob and trying to fetch self incorrectly.
plus can you also tell me how to access the params in a Model... is that even possible?
A has_one relationship will automatically add the :create_<relationship> and build_<relationship> methods. Checkout the rails guide.
You could try this:
after_create do
create_user_profile(:name => 'username')
end
Or more likely
after_create do
create_user_profile(:name => self.username)
end
I am trying to add a role to a specific user in my Rails Console via Heroku , but I am getting the error:
NoMethodError: undefined method `role=' for # .
If you take a look at the screenshot below the role you can see all of the columns in the User table that is available:
The command I am using from the rails console is below to assign the role to the user. I saw a similar post on StackOverflow about the same error but after the db migrate I still got the same error. Can someone point me in the right direction possibly?
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,
:confirmable
has_many :posts
def admin?
role == 'admin'
end
def moderator?
role == 'moderator'
end
end
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.
I am using Devise in my Ruby on Rails 3 application. I am trying to implement private messaging in my application and I came across this gem:
https://github.com/jongilbraith/simple-private-messages
I (accidentally) ran the following command.
rails generate simple_private_messages:model User Message
It created the Message model. But it changed some properties of my existing User model that I had generated using Devise using the following command:
rails generate devise User
Now, when I start my Ruby on Rails application I get this warning:
[WARNING] You provided devise_for :users but there is no model User defined in your application
And my Devise links have stopped working:
ActionView::Template::Error (undefined local variable or method `edit_user_registration_path' for #<#:0x1064c9490>):
Can someone please suggest how can I integrate the both or revert my changes if it is not possible to use them simultaneously?
I've followed these steps to install the gem:
rails generate devise:install
rails generate devise User
rails generate simple_private_messages:model User Message
Add this line (has_private_messages) to User Model:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :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_private_messages
end
Edit the routes.rb file, the order is important here, devise_for should be defined before the messages routes.
devise_for :users
resources :users do
resources :messages do
collection do
post :delete_selected
end
end
end
If you want the scaffold:
rails generate simple_private_messages:scaffold User Message
And remember to uncomment this (attr_accessor :to):
class Message < ActiveRecord::Base
is_private_message
# The :to accessor is used by the scaffolding,
# uncomment it if using it or you can remove it if not
attr_accessor :to
end