kodamarisa / osinotameni_yasetai

0 stars 0 forks source link

ユーザープロフィール画面でのエラー #36

Closed kodamarisa closed 5 months ago

kodamarisa commented 5 months ago

ActionView::Template::Error (undefined method `name' for nil:NilClass): 1: <% if user_signed_in? %> 2:

Your Profile

3:

Name: <%= current_user.name %>

4:

Email: <%= current_user.email %>

5: <% if current_user.customize.present? %> 6: <%= link_to "Edit Customize", edit_customize_path(current_user.customize) %>

app/views/users/profile.html.erb:3

- どの処理までうまく動いているのか
mvpレビューの依頼後、プロフィール画面に飛べていましたが、カスタマイズ画面へのリンクがうまくいかなかったため、修正をした後からこのエラーが出現しました。
- 該当のソースコード
users/profile.html.erb

<% if user_signed_in? %>

Your Profile

Name: <%= current_user.name %>

Email: <%= current_user.email %>

<% if current_user.customize.present? %> <%= link_to "Edit Customize", edit_customize_path(current_user.customize) %> <% else %> <%= link_to "Create Customize", new_customize_path %> <% end %> <%= link_to 'Logout', destroy_user_session_path, method: :delete %> <% end %>

CustomizesControlle

class CustomizesController < ApplicationController before_action :set_customize, only: [:edit, :update]

def new @customize = Customize.new end

def create @customize = Customize.new(customize_params) @customize.user_id = current_user.id

if @customize.save
  redirect_to user_profile_path, notice: 'Customization settings were successfully created.'
else
  render :new
end

end

def edit end

def update if @customize.update(customize_params) session[:calendar_color] = @customize.calendar_color redirect_to customizes_edit_path, notice: 'Customization settings were successfully updated.' else render :edit end end

private

def set_customize @customize = Customize.find_or_initialize_by(user_id: current_user.id) end

def customize_params params.require(:customize).permit(:calendar_color, :image) end end

UsersController

class UsersController < ApplicationController before_action :authenticate_user_or_line_user!

def profile if current_user @user = current_user else redirect_to new_user_session_path end end

private

def authenticate_user_or_line_user! if user_signed_in? authenticate_user! elsif line_user_signed_in? authenticate_line_user! else redirect_to new_user_session_path end end end

 Users::RegistrationsController

frozen_string_literal: true

class Users::RegistrationsController < Devise::RegistrationsController

before_action :configure_sign_up_params, only: [:create]

before_action :configure_account_update_params, only: [:update]

GET /resource/sign_up

def new

super

end

def choose if user_signed_in? redirect_to user_profile_path end end

POST /resource

def create super do |resource| if session[:current_calendar_id].present? calendar = Calendar.find(session[:current_calendar_id]) calendar.users << resource end end end

GET /resource/edit

def edit

super

end

PUT /resource

def update

super

end

DELETE /resource

def destroy

super

end

GET /resource/cancel

Forces the session data which is usually expired after sign

in to be expired now. This is useful if the user wants to

cancel oauth signing in/up in the middle of the process,

removing all OAuth session data.

def cancel

super

end

protected

If you have extra params to permit, append them to the sanitizer.

def configure_sign_up_params

devise_parameter_sanitizer.permit(:sign_up, keys: [:attribute])

end

If you have extra params to permit, append them to the sanitizer.

def configure_account_update_params

devise_parameter_sanitizer.permit(:account_update, keys: [:attribute])

end

The path used after sign up.

def after_sign_up_path_for(resource) user_profile_path end

The path used after sign up for inactive accounts.

def after_inactive_sign_up_path_for(resource)

super(resource)

end

end

customizes/edit.html.erb

Customize Page

<%= link_to 'Go to Bookmarks', bookmarks_path, class: 'btn btn-secondary' %>

<%= form_with(model: @customize, url: customizes_path, method: :patch) do |form| %>

<%= form.select :calendar_color, options_for_select(['Red', 'Blue', 'Green', 'Yellow']), {}, {class: 'form-control'} %>

<%= form.submit 'Save Changes', class: 'btn btn-primary' %> <% end %>


- エラーから考えられる原因
ユーザー情報がうまく渡っていないためnilになっていると考えられる。
- 試したこと
ログイン状態でプロフィールに飛ぼうとした->上記のエラー
ユーザー登録後のリダイレクトでプロフィールに飛ぼうとした->上記のエラー
何が原因でこうなったのかわからない状態です。
tmtkzk0823 commented 5 months ago

current_userは取得できていますでしょうか? また、

def profile
    if current_user
      @user = current_user
    else
      redirect_to new_user_session_path
    end
  end

で@userにcurrent_userを代入しているので、こちらも確認してみてください。

kodamarisa commented 5 months ago

current_userの取得についてはエラー文だとうまくいってないみたいですが、 ChatGPTにUsers::RegistrationsControllerとUsersControllerで取得できているか確認をしたら取得できていると返ってきました。 デバックを行ったところ

Started GET "/profile" for ::1 at 2024-05-23 17:20:31 +0900
  User Load (1.5ms)  SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 3], ["LIMIT", 1]]
Processing by UsersController#profile as HTML
Current user: #<User id: 3, email: "aaa@iueo", created_at: "2024-05-23 06:15:28.920310000 +0000", updated_at: "2024-05-23 06:15:28.920310000 +0000", name: "らんてくん">
  Rendering layout layouts/application.html.erb
  Rendering users/profile.html.erb within layouts/application
  Rendered users/profile.html.erb within layouts/application (Duration: 2.9ms | Allocations: 2540)
  Rendered layout layouts/application.html.erb (Duration: 3.1ms | Allocations: 2632)
Completed 500 Internal Server Error in 6ms (ActiveRecord: 0.0ms | Allocations: 4206)

ActionView::Template::Error (undefined method `name' for nil:NilClass):
    1: <% if user_signed_in? %>
    2:   <h1>Your Profile</h1>
    3:   <p><strong>Name:</strong> <%= current_user.name %></p>
    4:   <p><strong>Email:</strong> <%= current_user.email %></p>
    5:   <% if current_user.customize.present? %>
    6:     <%= link_to "Edit Customize", edit_customize_path(current_user.customize) %>

app/views/users/profile.html.erb:3

となっていたので、 customize が存在しないため、customize.present? のチェックが失敗してエラーになっていることが判明しました。

kodamarisa commented 5 months ago

profile.html.erbを

<% if user_signed_in? %>
  <h1>Your Profile</h1>
  <p><strong>Name:</strong> <%= @user.name %></p>
  <p><strong>Email:</strong> <%= @user.email %></p>
  <% if @user.customize.present? %>
    <%= link_to "Edit Customize", edit_customize_path(@user.customize) %>
  <% else %>
    <%= link_to "Create Customize", new_customize_path %>
  <% end %>
  <%= link_to 'Logout', destroy_user_session_path, method: :delete %>
<% end %>

この形にし、 ルーティングを

 # Customize routes
  resources :customizes, only: [:new, :edit, :create, :update]

こうしたら、上記のエラーがなくなり、プロフィール画面に飛ぶことができました!