Home>

I tried to display the profile information of the current user by transitioning from My Page
I wanted to create a page to display my profile, so I defined my own controller (mypage) and action (profile).
The following error occurred when trying to change pages from My Page of menu item.
* This is a bit of a spear-throwing question, but I hope you can forgive me for that.

NoMethodError in Mypage # profile
undefined method `username’ for nil: NilClass

<% = attachment_image_tag @user,: profile_image, fallback: "no-image-icon_person.jpg", class: "profile_image is-rounded"%> <% = @ user.username%> <% = link_to "edit", edit_user_path (@user), class: "button is-primary"%>

<% = attachment_image_tag @user,: profile_image, fallback: "no-image-icon_person.jpg", class: "profile_image is-rounded"%>
<% = @ user.username%>
<% = link_to "edit", edit_user_path (@user), class: "button is-primary"%>
<% = @ user.profile%>

Rails.application.routes.draw do
  devise_for: users
  # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
  root to: "home # index"
  resources: "users"
  resources: mypage, only: [: profile] do
    member do
      get'profile'
    end
  end
end

class MypageController
  • Answer # 1

    The instance variable in the controller@usersIt has become.
    In view@userIs referenced, so do as follows.

    @user = User.find (params [: id])

Related articles