Rails Association - User + Profile - ruby-on-rails

New to Rails and I have read the association manuals and tried to find an answer to why my association is not working.
I chose to separate the Profile from the user for ease of association to different user types and functions - I will have 3-4 different models for each type of user.
I have:
User model using Devise
Profile Model Called Temper
Dashboard View
If I am trying to call the information from the Profile model on the dashboard for the current user the code below "should" work if I am reading the guides right.
ERROR IS:
enter code hereundefined method `temp_phone_n' for #<User:0x007fbb8c62e4c8>
I see that it IS picking up the current user and trying to associate it but to no avail.
Can anyone tell me where I am going wrong.
Below:
user.rb
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,
:first_name, :last_name, :profile_name, :user_id
# attr_accessible :title, :body
has_one :temper
def full_name
first_name + " " + last_name
end
end
Temper.rb
class Temper < ActiveRecord::Base
attr_accessible :first_name, :last_name, :temp_about, :temp_avail, :temp_city, :temp_country, :temp_email, :temp_phone_d, :temp_phone_n, :temp_pors, :temp_skills, :temp_st_address_1, :temp_st_address_2, :user_id
has_one :user
end
Dashboard/index.html.erb
<div class="span5">
<ul>
<li><h4>Name:</h4> <%= current_user.full_name %></li>
<li><h4>Phone Number:</h4> <%= current_user.temp_phone_n %></li>
<li><h4>Email:</h4> <%= current_user.email %></li>
<li><h4>City:</h4> Toronto</li>
<li><h4>Member Since:</h4> <%= current_user.created_at.strftime("%B %d, %Y") %></li>
</ul>
</div>
Any assistance would be appreciated

You can access it like that:
current_user.temper.temp_phone_n
And your relationship is wrong, you can't write has_one to both side of the relationship
User.rb
has_one :temper
Temper.rb
belongs_to :user

Related

How to link/add models to each other

I'm trying to add idea(s) to a user. So far I added has_many to my user and belongs_to to my idea. And I added a foreign key to User using:
rails g migration Add_User_id_To_Ideas user_id:integer
Now, how do I assign the idea to the specific user, when a user creates a new idea?
I tried to work parallel to this example, but I'm a bit stuck.
user.rb
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_many :ideas
end
idea.rb
class Idea < ActiveRecord::Base
belongs_to :user
has_many :comments
mount_uploader :picture, PictureUploader
# {attr_accessor :Status}
enum status: [ :Draft, :Published]
end
Several ways to do this, but I think the most straight forward way would be to save the user_id in the ideas controller create action.
# def create inside ideas_controller
#idea.user_id = current_user.id
if #idea.save
# etc
Couple other options are a hidden field or before_save callback. Here's an example of passing through a hidden field. In your #idea form:
<%= f.hidden_field :user_id, value: current_user.id %>
This will add user_id to the params being saved. Make sure that :user_id is whitelisted in the permitted params at the bottom of your ideas controller.
To permit user_id in idea_params:
def idea_params
params.require(:idea).permit(:user_id, :also_add_other_params)
end

Getting NoMethodError in Statuses#show

i'm building a social media website with Ruby on Rails and i'm following instructions from teamtreehouse.com . Every time i make a new status or go to the index page i get this message
undefined method `full_name' for nil:NilClass
<p>
<strong>Name:</strong>
<%= #status.user.full_name %>
</p>
I coded my pages to show the full name with this
<%= #status.user.full_name %>
and
<%= status.user.full_name %>
for the index page.
I have included all of these snippets of code into my project as well
status.rb
class Status < ActiveRecord::Base
attr_accessible :content, :user_id
belongs_to :user
end
user.rb
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :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,
:first_name, :last_name, :profile_name
# attr_accessible :title, :body
def full_name
first_name + " " + last_name
end
end
I've tried using rake db:reset and rake db:migrate but nothing seems to solve the error problem. Thank you for your help!
As per the error,
undefined method `full_name' for nil:NilClass
which means that
#status.user = nil
i.e., the particular status instance that you are looking at(#status) doesn't have an associated user record.
You can verify it by going to rails console
Status.find(pass_the_id) ## pass the id of #status instance
You will notice that user_id is nil for that record.
Set the value to an existing user_id and try again.
Note: you might need to take a deeper look at how you are storing a status in your application. I suppose you are missing to store user_id attribute there
Looks like you'd need to include has_many :statuses in the User model
You may also wish to use the .delegate() method:
class Status < ActiveRecord::Base
attr_accessible :content, :user_id
belongs_to :user
delegate :full_name, to :user, prefix: :author
end
This should allow you to call #status.author_full_name

Undefined local variable or method 'rolify'

In my Rails project, I am trying to set up user authentication and user roles using the following Gems:
Devise
Cancan
Rolify
I had Devise up and working perfectly, but then when I went to set up CanCan and Rolify, I get this error in the browser:
NameError in Home#index
Showing C:/Sites/JustManage/app/views/devise/menu/_registration_items.html.erb where line #1 raised:
undefined local variable or method `rolify' for #<Class:0x3f48660>
Extracted source (around line #1):
1: <% if user_signed_in? %>
2:
3: <li>
4: <%= link_to('Edit registration', edit_user_registration_path) %>
Trace of template inclusion: app/views/layouts/application.html.erb
Rails.root: C:/Sites/JustManage
user.rb
class User < ActiveRecord::Base
rolify
# 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, :subdomain, :first_name, :last_name
# Make sure certain attributes are unique
validates_uniqueness_of :email, :case_sensitive => false
end
role.rb
class Role < ActiveRecord::Base
has_and_belongs_to_many :users, :join_table => :users_roles
belongs_to :resource, :polymorphic => true
scopify
end
rolify.rb
Rolify.configure do |config|
config.use_dynamic_shortcuts
end
Let me know if you need me to post contents from any of my other files!
I had the same issue. Restarting my server fixed it!
'When in doubt, ctrl-c out!'
Is it safe to assume that you installed and subsequently required the rolify gem properly?...
Please verify that rolify is listed when you run gem list and that you have added require 'rolify' at the top of your user.rb file.

How to get the current user that is logged in via active admin?

I would like to know how to get the current user that is logged in via the active admin GUI?
Homepage: http://www.activeadmin.info/
Thanks in advance
MODEL
admin_user.rb
class AdminUser < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :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, :id , :admin_permission
# attr_accessible :title, :body
has_one :admin_permission
validates_uniqueness_of :email
def self.current_logged_user
# ?
end
end
UPDATE
When I try to use the method 'current_user' in dashboard.rb or any active admin related file, apparently it is not implemented.
OUTPUT
undefined local variable or method `current_user' for
The active admin user is called AdminUser by default. The corresponding devise helper method is therefore current_admin_user.
reload the page and see in your terminal, in this case, puts the correct current_user logged email.
index do
column :name
column :email
column :slug
column :partner
puts current_user.email
default_actions
end
ActiveAdmin v1.x
index do
selectable_column
column :id
column :name
column :current_user_email do
current_user.try(:email)
end
actions
end

Not able to use new model with devise

I have 2 models in my application ( Using rails 3.2.5)
1) User(From devise)
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
# attr_accessible :title, :body
has_many :profiles
end
2) Profiles.
class Profile < ActiveRecord::Base
belongs_to :user
attr_accessible :description, :name, :product, :image, :price
mount_uploader :image, ImageUploader
validates_presence_of :name, :product, :image, :price
end
There are many profiles of one user .
GOAL : I want to display only the profiles related to each user , when they login.
By reading rails document we can create a new profile for a user by (Code below), But this gives an error
"undefined method `Profile' for nil:NilClass"
Can somebody help me to define my controller's index,show,new,edit,create,update ,destroy or show me one and i can do it for all.
def new
#profile = #user.Profile.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: #profile }
end
end
You are receiving this error because the #user is not initialized and is equal to nil so it has no method named profile, you should be using current_user helper of devise to get the current user #user = current_user.

Resources