2

I am a rookie in Rails. I am using Rails 4 and I could not find how they do this or what it is called.

I got this idea from devise where you can use devise and implement such thing in your application.html.erb file:

<% if user_signed_in? %>
      Logged in as <strong><%= current_user.email %></strong>.

Where user is the devise model. However when I try to search for user_signed_in or current_user variable, I cannot find it at all!

So essentially what I want to do is link this user model (which is used for devise) with another model that I created called profile. These models are linked by their ids, and if user has not created a profile, then simply ask user to create his/her profile. To do that, I've written this to application.html.erb:

<% if user_signed_in? && (current_profile.id != current_user.id)? %>
    <%= link_to 'You have not created your profile! Please create your profile first.', update_profile_index_path, :class => 'navbar-link'  %>
<% else %>
    <%= yield %>
<% end %>

Which does not work as expected because I have not defined current_profile. The error that I am getting is:

undefined local variable or method `current_profile' for #<#<Class:0x000000044d6c60>:0x00000005d64110>

My question is, how do I create a variable named current_profile that would contain the current profile, like current_user that devise does?

8
  • Why don't you simply use current_user.profile ? Commented Aug 27, 2014 at 13:21
  • current_user is a devise model. It does not have a profile? Commented Aug 27, 2014 at 13:21
  • if you define has_one profile on the user model, then current_user.profile will work Commented Aug 27, 2014 at 13:22
  • I already did that for both and it doesn't work. I am getting undefined method `profile?' for #<User:0x00000002f82c88> error if I do that Commented Aug 27, 2014 at 13:22
  • then you need to define def profile?; return self.profile; end on your User model Commented Aug 27, 2014 at 13:23

2 Answers 2

3

The usual setup for this is to add a Profile model with a user_id:integer field.

Define an assocition on the User model

has_one :profile

Then you can access it directly using

current_user.profile
Sign up to request clarification or add additional context in comments.

Comments

3

You can do the following:

class User
  has_one :profile
  # ...

class Profile
  belongs_to :user
  # ...

module ApplicationHelper # app/helpers/application_helper.rb
  def current_profile
    @current_profile ||= current_user.try(:profile)
    @current_profile
  end
  # ...

# view
<% if user_signed_in? && current_profile.blank? %>
  <%= link_to 'You have not created your profile! Please create your profile first.', update_profile_index_path, :class => 'navbar-link'  %>
<% else %>
  <%= yield %>
<% end %>

11 Comments

I have has_one in Profile as well because I want to strict that one profile has only one user?
It worked like a charm! And this kind of thing was what I was wanting at the first place!
@SarpKaya where do you store the foreign key - is there a profile_id field in your users table or a user_id field in your profiles table?
The model holding the belongs_to :something part is the model that holds the column something_id. In your case User has_one :profile and Profile belongs_to :user
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.