ormprog / rails_ama

Ruby/Rails - Ask Me Anything
MIT License
2 stars 0 forks source link

localhost show full name perfecly But Heroku didn't #136

Closed ansary14-zz closed 3 years ago

ansary14-zz commented 3 years ago

Local host shows full name perfectly. After editing the profile.

Screenshot from 2020-11-27 01-42-35

But Heroku didn't show, its show Anonymous which was for such case where a first name or last name doesn't set by the user.

Screenshot from 2020-11-27 01-46-56

ansary14-zz commented 3 years ago

Here is Heroku log file finance-tracker-ayub-logs-1606417452520.txt

wasifhossain commented 3 years ago

could you share the code that renders the name

ansary14-zz commented 3 years ago

<li class="nav-item user-name"> <%= fa_icon 'user' %> <%= current_user.full_name %> </li>

ansary14-zz commented 3 years ago
  def full_name
    return "#{first_name} #{last_name}" if first_name || last_name
    "Anonymous"
  end
ansary14-zz commented 3 years ago

this application created with help by devise gem

wasifhossain commented 3 years ago

could you share the first_name and last_name of the user from rails console on Heroku,

e.g. User.find(:id).first_name and User.find(:id).last_name

ansary14-zz commented 3 years ago

In Heroku rails console both are nil. All the users first_name and the last_name are nil

wasifhossain commented 3 years ago

def full_name return "#{first_name} #{last_name}" if first_name || last_name "Anonymous" end

thats the reason why full_name returned Anonymous

wasifhossain commented 3 years ago

according to the logic, "#{first_name} #{last_name}" will be returned only when at least one of first_name or last_name are non-nil values.

that also implies if at least of them are empty string, still "#{first_name} #{last_name}" will be used

wasifhossain commented 3 years ago

could you clarify when you would like to see "#{first_name} #{last_name}" and when Anonymous?

ansary14-zz commented 3 years ago

When I send you a peek video. This time I update profile (Edit profile) with first name And last name But after the update button press, it shows the message that updated successfully but the full name doesn't show.

ansary14-zz commented 3 years ago

But now when I repeat the process again in Heroku Then it returns the full name. I didn't change anything....!

wasifhossain commented 3 years ago

so its now working, you think?

ansary14-zz commented 3 years ago

could you clarify when you would like to see "#{first_name} #{last_name}" and when Anonymous?

When the user has a first name or last name then it shows the full name (first name or last name or both) If both missing then Anynomous

ansary14-zz commented 3 years ago

so its now working, you think?

Yes It's now working

ansary14-zz commented 3 years ago

I think I need some patient after Heroku push

wasifhossain commented 3 years ago

could you clarify when you would like to see "#{first_name} #{last_name}" and when Anonymous?

When the user has a first name or last name then it shows the full name (first name or last name or both) If both missing then Anynomous

ok its clear. nevertheless, you should care about the empty string too.

when you submit the form with empty values, empty strings '' are inserted in place of nil in the corresponding fields.

the following logic will match nil values but fail on empty strings

if first_name || last_name

if you want to match either nil or empty strings, you have to write it as

if first_name.present? || last_name.present?
wasifhossain commented 3 years ago

I would have written the logic as

def full_name
  name = [first_name.presence, last_name.presence].compact.join(' ').presence

  name || 'Anonymous'
end

cc @ansary14