質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

解決済

2回答

1754閲覧

NoMethoderrorの解決に困っています

lwbox

総合スコア1

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

0クリップ

投稿2021/02/11 13:46

前提・実現したいこと

投稿機能がある簡易的なアプリを作っています。
サーバーに接続すると以下のようなエラーメッセージが出てしまいました。

発生している問題・エラーメッセージ

NoMethodError in Prototypes#index undefined method `name' for nil:NilClass

該当のソースコード

エラーで指摘されているのが以下の5行目のnameです。
app/views/prototypes/index.html.erb

ruby

1<main class="main"> 2 <div class="inner"> 3 <div class="greeting"> 4 こんにちは、 5 <%= link_to " #{current_user.name} さん", "/users/#{current_user.id}", class: :greeting__link%> 6 </div> 7 <div class="card__wrapper"> 8 <%= render partial: "prototype", collection: @prototypes %> 9 </div> 10 </div> 11</main>

app/controllers/prototypes_controller.rb

ruby

1class PrototypesController < ApplicationController 2 before_action :authenticate_user!, only: [:new, :edit, :destroy] 3 4 def index 5 @prototypes = Prototype.all 6 end 7 8 def new 9 @prototype = Prototype.new 10 end 11 12 def create 13 @prototype = Prototype.new(prototype_params) 14 if @prototype.save 15 redirect_to root_path 16 else 17 render :new 18 end 19 end 20 21 def show 22 @prototype = Prototype.find(params[:id]) 23 @comment = Comment.new 24 @comments = @prototype.comments.includes(:user, :prototype) 25 end 26 27 def edit 28 @prototype = Prototype.find(params[:id]) 29 unless user_signed_in? 30 redirect_to root_path 31 end 32 end 33 34 def update 35 @prototype = Prototype.find(params[:id]) 36 if @prototype.update(prototype_params) 37 redirect_to prototype_path 38 else 39 render :edit 40 end 41 end 42 43 def destroy 44 prototype = Prototype.find(params[:id]) 45 if prototype.destroy 46 redirect_to root_path 47 end 48 end 49 50 private 51 def prototype_params 52 params.require(:prototype).permit(:concept, :image, :title, :catch_copy).merge(user_id: current_user.id) 53 end 54end 55

試したこと

このエラー文をそのまま翻訳すると、空のものに対してnameメソッドを使用することはできない。そしてnameの前のcurrent_userがnilと認識されている。ということだと思います。
prototypesコントローラのindexアクションが何かおかしいのではと思いましたが多分これは大丈夫だと思います。
current_userのところが@userなのでは?と思い、それも試しましたがだめでした。

補足情報(FW/ツールのバージョンなど)

indexのファイルにおいて、8行目のrenderメソッドで呼び出している部分テンプレートはこちらになります。

ruby

1<div class="card"> 2 <%= link_to image_tag(prototype.image, class: :card__img), prototype_path(prototype.id)%> 3 <li> 4 <%= link_to '詳細', prototype_path(prototype.id), method: :get %> 5 </li> 6 <div class="card__body"> 7 <%= link_to prototype.title, root_path, class: :card__title%> 8 <p class="card__summary"> 9 <%= prototype.catch_copy %> 10 </p> 11 <%= link_to "by #{current_user.name}", "/users/#{current_user.id}", class: :card__user %> 12 </div> 13</div>

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

before_action :authenticate_user!, only: [:new, :edit, :destroy] なので、indexのときはLogin不要です。この場合 current_userはnil

indexもLogin必要とするか、<div class="greeting"> の所を if current_user で場合分けしてください

投稿2021/02/11 23:34

winterboum

総合スコア23347

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

lwbox

2021/02/12 01:33

ご回答ありがとうございます。こんな感じで修正してみたのですが、今度は部分テンプレートでも同じエラーが出てしまいます。 ```ruby <% if user_signed_in? %> <div class="greeting"> こんにちは、 <%= link_to " #{current_user.name} さん", "/users/#{current_user.id}", class: :greeting__link%> </div> <% end %> ``` 追加で修正する箇所はありますか?
lwbox

2021/02/12 02:03

解決しました。大丈夫です。
guest

0

自己解決

下のようにindex.html.erbを場合分けしなければならなかったのと、

ruby

1 <% if user_signed_in? %> 2 <div class="greeting"> 3 こんにちは、 4 <%= link_to "#{current_user.name} さん", "/users/#{current_user.id}", class: :greeting__link%> 5 </div> 6 <% end %>

エラーが出てた補足の方で記述したrenderメソッドで呼び出している部分テンプレートに関しては

ruby

1<%= link_to "by #{prototype.user.name}", "/users/#{prototype.user.id}", class: :card__user %>

と、プロトタイプの中でのユーザーであるということを記述しなければなりませんでした。
他のファイルでも、コメントの中であれば

ruby

1<%= link_to comment.user.name, "/users/#{comment.user.id}", class: :comment_user %>

と記述を直しました。

投稿2021/02/12 02:12

lwbox

総合スコア1

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問