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

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

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

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

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

Q&A

解決済

2回答

314閲覧

マイページのユーザー名を表示したい

tagomaru025

総合スコア6

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

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

0グッド

0クリップ

投稿2019/11/08 16:28

前提・実現したいこと

マイページの名前をユーザー毎に変更して表示させたいです。

Ruby on Rails 6.0.1 で記事投稿サイトを作成しています。

マイページ画面(プロフィール画面のようなもの)でユーザー名を表示する際に以下のエラーが発生しました。

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

NoMethodError in Users#show
Showing /home/vagrant/environment/media_a/app/views/users/show.html.erb where line #1 raised:

undefined method `name' for nil:NilClass
Extracted source (around line #1):

<h1><%= @user.name %>さんのマイページ</h1> <h3>記事一覧</h3> <div class="mb-3"> <table class="table table-hover"> <thead class="thead-light">

Rails.root: /home/vagrant/environment/media_a

該当のソースコード

show.html.erbのコード

<h1><%= @user.name %>さんのマイページ</h1> <h3>記事一覧</h3> <div class="mb-3"> <table class="table table-hover"> <thead class="thead-light"> <tr> <th scope="col"><%= t("activerecord.attributes.article.id") %></th> <th scope="col"><%= t("activerecord.attributes.article.title") %></th> <th scope="col"><%= t("activerecord.attributes.article.created_at") %></th> <th scope="col"><%= t("activerecord.attributes.article.updated_at") %></th> <th scope="col">その他</th> </tr </thead> <tbody class="bg-white"> <% @articles.each do |articles| %> <tr> <td><%= articles.id %></td> <td><%= link_to articles.title, article_path %></td> <td><%= articles.created_at %></td> <td><%= articles.updated_at%></td> <div class="e_d_button"> <a><%= render partial: 'articles/e_d_btn', locals: { article: @article } %></a> </div> </tr> <% end %> </tbody> </table> </div>

users_controller.rbのコード

class UsersController < ApplicationController

def index
@user = current_user.find(params[:id])
end

def show
@user = User.find_by(id: params[:user_id])
end

def edit
@user = current_user.find(params[:id])
end

def destroy
@user = User.find(params[:id])
@user.destroy
redirect_to user_url(@user)
flash[:notice] = "記事『#{@articles.title}』を削除しました。"
end

end

試したこと

インターネットで調べ、controllerのshowが原因だと考えたので、@user = User.find(params[:id])も試していました。しかし、別の場所でエラーが出たので断念しました。この他に何か良い方法はございますでしょうか?
初投稿ゆえ、分かりにくい場所があるとございますが、ご教授の程よろしくお願いします。

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

ここにより詳細な情報を記載してください。

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

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

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

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

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

guest

回答2

0

ベストアンサー

showメソッドの
@user = User.find_by(id: params[:user_id])
でuser_idなるものから検索していますが、ここは単にparams[:id]では?

投稿2019/11/15 06:48

SakStar

総合スコア24

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

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

tagomaru025

2019/11/19 05:18

ご回答ありがとうございます! その後、params[:id]で試したところ上手く動作しました! 本当にありがとうございます!
guest

0

showを呼ぶviewとshowの内容がマッチしていないから、@userが採れないのです。
showを呼ぶ所はどういうcodeになっていますか

投稿2019/11/09 00:08

winterboum

総合スコア23284

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

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

tagomaru025

2019/11/09 13:53

以下がshow.html.erbの全コードとなっています。 <h1>で囲んでいる場所がshowを呼ぼうとしているところです。 <h1><%= @user.name %>さんのマイページ</h1> <h3>記事一覧</h3> <div class="mb-3"> <table class="table table-hover"> <thead class="thead-light"> <tr> <th scope="col"><%= t("activerecord.attributes.article.id") %></th> <th scope="col"><%= t("activerecord.attributes.article.title") %></th> <th scope="col"><%= t("activerecord.attributes.article.created_at") %></th> <th scope="col"><%= t("activerecord.attributes.article.updated_at") %></th> <th scope="col">その他</th> </tr </thead> <tbody class="bg-white"> <% @articles.each do |articles| %> <tr> <td><%= articles.id %></td> <td><%= link_to articles.title, article_path %></td> <td><%= articles.created_at %></td> <td><%= articles.updated_at%></td> <div class="e_d_button"> <a><%= render partial: 'articles/e_d_btn', locals: { article: @article } %></a> </div> </tr> <% end %> </tbody> </table> </div>
winterboum

2019/11/09 21:31

それは show です。ほしいのはそれを呼ぶ画面のviewです。 あと、codeはコメントに書かず、質問に追記して下さい
tagomaru025

2019/11/11 07:29

失礼いたしました。その後、もう一度試行錯誤した結果、自己解決することができました。ありがとうございました。 codeの件は今後気をつけます。教えて頂きましてありがとうございます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問