I am getting an error on my application undefined method 'preference' for #<User:0x007fb3cc1c3b80>. Following were my accounts controller:
class AccountsController < ApplicationController
before_action :authenticate_user!
def edit
#render html: 'Edit your account'
render component: 'AccountsEdit', props: {
preference: PreferenceSerializer.new(current_user.preference)
}, tag: 'div'
end
def update
#preference = current_user.preference
if #preference.update_attributes(preference_params)
render json: { data: 'SUCCESS!' }
else
render json: { data: 'FAIL!' }
end
end
private
def preference_params
params.require(:preference).permit(:display_name, :notify_on_answer, :daily_digest)
end
end
And my User and Preference model:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
end
class Preference < ActiveRecord::Base
belongs_to :user
end
Seems all look alright, but i keep getting the same error. Am i missing something here? Thanks!!
Inside your User model add the relation to the Preference model:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_a: :preference # Or has_many: :preferences
end
Related
I'm trying to create a record for the associated model in rails console in the form Contact.first.to_do_items.create!() and I have ActiveRecord::RecordInvalid Validation failed: contact must exist
Models:
class Contact < ApplicationRecord
acts_as_token_authenticatable
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
has_many :to_do_items
end
class ToDoItem < ApplicationRecord
belongs_to :сontact
end
It works well when I'm using optional: true in the ToDoItem class but I'm curious why it's not creating a record with the code listed above.
I want that when the user sign up (Devise) it create its first "Profile" account at the same time of registration form.
I tried with "fields_for" but it's not working. I can add an hidden_field in the registration form because the "user" is not created yet.
How can I do?
Model
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable,
:trackable
class Profile < ApplicationRecord
belongs_to :user #creator
Views (Sign up)
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= f.fields_for :profiles, resource.profiles.build do |s| %>
<%= s.text_field :name%>
<% end %>
<% end %>
You could have a call back in the model.
Like after_save and then create a profile.
You can look up callbacks here: https://guides.rubyonrails.org/active_record_callbacks.html#available-callbacks
And it could look like this:
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable,
:trackable
after_save :create_profile
def create_profile
Profile.create(user: self,...)
end
end
Another option would be to generate a devise controller yourself. You can follow the steps from their readme: https://github.com/plataformatec/devise/wiki/Tool:-Generate-and-customize-controllers
And as they suggest, tweak the create method, by adding
Profile.new(...)
def create
super #inherits from devise controllers and create the user model
# now you can add your own code creating the Profile
Profile.new(user: resource, ...)
end
my user.rb model contains:
class User < ApplicationRecord
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,
:confirmable, :lockable, :timeoutable, :omniauthable,
:jwt_authenticatable, jwt_revocation_strategy: JWTBlacklist
def send_devise_notification(notification, *args)
devise_mailer.send(notification, self, *args).deliver_later
end
end
I am using devise-jwt gem to signin for my rails api.
my JWTBlacklist.rb model contains:
class JwtBlacklist < ApplicationRecord
include Devise::JWT::RevocationStrategies::Blacklist
self.table_name = 'jwt_blacklist'
end
Your User class is looking for JWTBlacklist, but your file is defining JwtBlacklist. You need to change one of those to match the other.
I am new to stackoverflow and i want to implement user with multiple roles .
I had started with rolify gem . I had generated 3 devise users manager , owner , user (visitor).
Association used for my application is
class Role < ApplicationRecord
has_and_belongs_to_many :users, :join_table => :users_roles
belongs_to :resource,
:polymorphic => true,
:optional => true
validates :resource_type,
:inclusion => { :in => Rolify.resource_types },
:allow_nil => true
scopify
end
class User < ApplicationRecord
rolify
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
after_create :assign_default_role
def assign_default_role
self.add_role(:visitor) if self.roles.blank?
end
end
class Owner < User
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
after_create :assign_default_role
def assign_default_role
self.add_role(:owner) if self.roles.blank?
end
end
class Manager < User
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
after_create :assign_default_role
def assign_default_role
self.add_role(:moderator) if self.roles.blank?
end
end
My concern is i am using rolify gem to assign role but i want to keep manager , owner , visitor table separate but if i didn't use Single table inheritance then how can i able to implement roles and keep table separate
How can I create a customer class I want to be a Devise User with a role = 1.
I have my User model:
class User < ActiveRecord::Base
enum role: [:user, :customer, :admin, :producer]
after_initialize :set_default_role, :if => :new_record?
def set_default_role
self.role ||= :user
end
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable, :registerable
devise :invitable, :database_authenticatable, :confirmable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
end
And my Customer model
class Customer < User
end
I can specify in the customersController what is a customer like this :
# GET /customers
def index
#customers = Customer.where(:role => 1).page(params[:page])
end
But How can I modify my Customer model to self know he is a User with role = 1 so I can only get my Customers like this :
#customers = Customer.all
if you exactly need Customer.all you should looking for STI(single table inheritance) which already resolve what you trying to do