0

How to show user's first_name in page like "Hello, current_user.first_name", my schema

create_table "users", force: :cascade do |t|
    t.string "email", default: "", null: false
    t.string "encrypted_password", default: "", null: false
    t.string "first_name", default: "", null: false
    t.string "last_name", default: "", null: false
    t.string "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer "sign_in_count", default: 0, null: false
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.string "current_sign_in_ip"
    t.string "last_sign_in_ip"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["email"], name: "index_users_on_email", unique: true
    t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
  end

when i tried to do in my page <span>Hello, <%= current_user.email %></span> it's all ok, but whet i tried <span>Hello, <%= current_user.first_name %></span> it's show nothing. Where is my mistake?

djaszczurowski
  • 4,385
  • 1
  • 18
  • 28
asda111sd
  • 29
  • 6
  • Please share the basic details of the current user that you have stored. – Vishal Jan 05 '18 at 09:34
  • @asda111sd if you know the `current_user`'s `id` then check at rails console `User.find(1).first_name.present?` here i have given `1` user id you can check it by current_user id – Anand Jan 05 '18 at 09:39
  • current_user.id works correctly – asda111sd Jan 05 '18 at 09:42
  • @asda111sd You are saying same thing works and also doesn't work at the end. Please correct it. – Nabin Paudyal Jan 05 '18 at 09:44
  • @asda111sd no you did not get me i mean to say on rails console check User.find(current_user_id).first_name.present? if it returns true of false? and replace current_user_id with your current_user.id – Anand Jan 05 '18 at 09:44
  • `first_name` and `last_name` inserting properly when you inserted? check this first then try to login `current_user.first_name` – fool-dev Jan 05 '18 at 09:45
  • @asda111sd could you share your `create action` of users? – Anand Jan 05 '18 at 09:57
  • I'm quite sure you are not saving the first_name value to user (correctly). If you go to rails console and add the first_name attribute and save the user then I believe you will see the user's first_name in your browser too. – Andres Jan 05 '18 at 10:07
  • @asda111sd, Did you checked in your database that first_name is present or not? – sam Jan 05 '18 at 10:58
  • Am i the only one to find both the statements exactly same `Hello, <%= current_user.email %>` ? – Jagdeep Singh Jan 05 '18 at 11:28
  • @gabbar that give me console User.find(1).first_name.present? User Load (13.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]] => false – asda111sd Jan 05 '18 at 11:38
  • @asda111sd thats clearly mean that you ur user don't have first_name. so kindly review your signup follow where you are saving user information and check if first_name is being saved or not ? – Anand Jan 05 '18 at 11:43
  • @Gabbar that's from my registration view
    <%= f.label :first_name %>
    <%= f.text_field :first_name, autofocus: false %>
    – asda111sd Jan 05 '18 at 11:46
  • @Gabbar if it's important that i have not any users controller because devise have not generate it – asda111sd Jan 05 '18 at 11:47
  • @asda111sd if you are following devise signup page than devise by default provides only email and password field with signup page, so you need to add more fields like first_name, last_name with signup page to get first_name here. here you can generate devise signup page and add fields there like first_name, last_name and .. so on. – Anand Jan 05 '18 at 11:52

2 Answers2

0

The problem here is in your schema, you have t.string "first_name", default: "", null: false which saves the empty string in your database, if you don't pass the first_name.I'm quite sure you are not saving the first_name value to user anyhow and it is by default set to "". When you try to access <span> <%= "Hello, #{current_user.first_name}" %></span> here it return empty string.Here user is present as if user is not present then it would have given error as first_name for nil:NilClass.

add this to your ApplicationController

    before_action :configure_permitted_parameters, if: :devise_controller?

  protected

  def configure_permitted_parameters
    permit_attrs(%i[first_name last_name])
  end

  def permit_attrs(attrs)
    %i[sign_up account_update].each do |action|
      devise_parameter_sanitizer.permit(action, keys: attrs)
    end
  end

in your new.html.erb you have two first name field change one as below:

  <div class="field">
    <%= f.label :first_name %><br />
    <%= f.text_field :first_name, autofocus: false, class: "form-control" %>
  </div>

to

  <div class="field">
    <%= f.label :last_name %><br />
    <%= f.text_field :last_name, autofocus: false, class: "form-control" %>
  </div>
Ritesh Ranjan
  • 1,012
  • 9
  • 16
0

So if you are seeking to display user information, this is how I handle that.

Display User Input Fields

<p> Hello <%= @user.first_name %>

Now, if you want to be able to show user input information IF they have it.

If User Filled Out Form

<% if @user.first_name? %>
  <p>Hello, <%= @user.first_name %></p>
<% else %>
  <p>Hello Anonymous User</p>
<% end %>