Search code examples
refinerycms

How to get the name of the current user in Refinery?


In RefineryCMS, for some functionality I have to store the present logged in user's first name and last name.

How to get logged in user's first name and last name?


Solution

  • While going through this implementation, I found that refinery user table does not have column to store first name, last name or any basic details about user.

    So, first step is to create a migration file having first_name, last_name etc as per requirement.

    $ rails generate migration AddFirstNameLastNameToRefineryUsers first_name:string last_name:string

    Then run

    $ rake db:migrate

    Now, create a decorators for user model.

    Path :: /app/decorators/models/refinery/user_decorator.rb

    Create a decorator : Refinery Documentation Link for Creating Decorator

    Add this line inside that model

    attr_accessible :first_name, :last_name

    validates :first_name,:last_name, :presence => true

    Now add this line at view area

    • /app/views/users/new.html.erb
      <div class='field'>
        <%= f.label :first_name %>
        <%= f.text_field :first_name, :class => 'larger widest' %>
      </div>
      <div class='field'>
        <%= f.label :last_name %>
        <%= f.text_field :last_name, :class => 'larger widest' %>
      </div>
    

    Now, Rails will save the values, when user makes first time login.