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

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

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

Cloud9は、クラウドからのプログラミングが可能になるWebサービス。IDEとしての機能が搭載されており、GitHubやHerokuなど他ツールとの連携も可能です。ブラウザ上で動くため、デバイスに関係なく開発環境を準備できます。

Ruby on Rails 5

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

Ruby

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

Q&A

解決済

1回答

1540閲覧

エラー nil:NilClass が解決できない

sakana-suki

総合スコア14

Cloud9

Cloud9は、クラウドからのプログラミングが可能になるWebサービス。IDEとしての機能が搭載されており、GitHubやHerokuなど他ツールとの連携も可能です。ブラウザ上で動くため、デバイスに関係なく開発環境を準備できます。

Ruby on Rails 5

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

Ruby

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

0グッド

0クリップ

投稿2019/09/04 04:15

編集2019/09/04 04:23

前提・実現したいこと

deviseでログイン機能を作った後、全ユーザーを表示するusers/indexページを作りたいです。

経緯

railsチュートリアル10章を参考にしつつ、ユーザー一覧ページを作るために、DBをリセット後、gem faker(rails db:seed)を使ってユーザー数を増やし、成功しました(この時はusers/indexページはちゃんと機能していました)が、ログインできなくなってしまいました。

そこで、gem fakerの使用を一旦諦め、再度 rails db:migrate:reset して、手動でユーザー登録し直し
users/indexにアクセスすると

Showing /home/ec2-user/environment/insta-clone/app/views/users/show.html.erb undefined method `profile_photo' for nil:NilClass

と、エラーがでしまいました。
また、エラーが出ている個所は下記の二行目です。

def avatar_url(user) return user.profile_photo unless user.profile_photo.nil? gravatar_id = Digest::MD5::hexdigest(user.email).downcase "https://www.gravatar.com/avatar/#{gravatar_id}.jpg" end

何故、userがnilになっているかよくわかりません。。

また、users/indexにアクセスすると、show.html.erbに関してエラーが出るが、ユーザーのプロフィール画面にusers/1 (←ユーザーiD)と直打ちしてアクセスするとshow.html.erbでエラーは出ないので更に???となっています。

Railsの仕組みが全体的にまだまだよくわからず、つながりが見えてきません。
どなたかエラー解決法など教えてくださると幸いです。

該当のソースコード

/insta-clone/app/helpers/application_helper.rb

module ApplicationHelper def avatar_url(user) return user.profile_photo unless user.profile_photo.nil? gravatar_id = Digest::MD5::hexdigest(user.email).downcase "https://www.gravatar.com/avatar/#{gravatar_id}.jpg" end end

/insta-clone/app/views/users/show.html.erb

<div class="profile-wrap"> <div class="row"> <div class="col-md-4 text-center"> <%= image_tag avatar_url(@user), class: "round-img" %> </div> <div class="col-md-8"> <div class="row"> <h1><%= @user.name %></h1> <% if @user == current_user %> <%= link_to "プロフィールを編集", edit_user_registration_path, class: "btn btn-outline-dark common-btn edit-profile-btn" %> <button type="button" class="setting" data-toggle="modal" data-target="#exampleModal"></button> <% end %> <div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true"> <div class="modal-dialog" role="document"> <div class="modal-content"> <div class="modal-header"> <h5 class="modal-title" id="exampleModalLabel">設定</h5> <button type="button" class="close" data-dismiss="modal" aria-label="Close"> <span aria-hidden="true">×</span> </button> </div> <div class="list-group text-center"> <%= link_to "サインアウト", destroy_user_session_path, method: :delete, class: "list-group-item list-group-item-action" %> <%= link_to "キャンセル", "#", class: "list-group-item list-group-item-action", "data-dismiss": "modal" %> </div> </div> </div> </div> </div> <% if @user == current_user %> <div class="row"> <p> <%= @user.email %> </p> </div> <% end %> </div> </div> </div>

ルーティングはこんな感じです

Prefix Verb URI Pattern Controller#Action new_user_session GET /users/sign_in(.:format) users/sessions#new user_session POST /users/sign_in(.:format) users/sessions#create destroy_user_session DELETE /users/sign_out(.:format) users/sessions#destroy user_facebook_omniauth_authorize GET|POST /users/auth/facebook(.:format) users/omniauth_callbacks#passthru user_facebook_omniauth_callback GET|POST /users/auth/facebook/callback(.:format) users/omniauth_callbacks#facebook new_user_password GET /users/password/new(.:format) devise/passwords#new edit_user_password GET /users/password/edit(.:format) devise/passwords#edit user_password PATCH /users/password(.:format) devise/passwords#update PUT /users/password(.:format) devise/passwords#update POST /users/password(.:format) devise/passwords#create cancel_user_registration GET /users/cancel(.:format) users/registrations#cancel new_user_registration GET /users/sign_up(.:format) users/registrations#new edit_user_registration GET /users/edit(.:format) users/registrations#edit user_registration PATCH /users(.:format) users/registrations#update PUT /users(.:format) users/registrations#update DELETE /users(.:format) users/registrations#destroy POST /users(.:format) users/registrations#create sign_in GET /sign_in(.:format) users/sessions#new sign_out GET /sign_out(.:format) users/sessions#destroy root GET / posts#index user GET /users/:id(.:format) users#show user_index GET /users/index(.:format) users#index post_photos POST /posts/:post_id/photos(.:format) photos#create post_likes POST /posts/:post_id/likes(.:format) likes#create post_like DELETE /posts/:post_id/likes/:id(.:format) likes#destroy post_comments POST /posts/:post_id/comments(.:format) comments#create post_comment DELETE /posts/:post_id/comments/:id(.:format) comments#destroy posts GET /posts(.:format) posts#index POST /posts(.:format) posts#create new_post GET /posts/new(.:format) posts#new post GET /posts/:id(.:format) posts#show DELETE /posts/:id(.:format) posts#destroy rails_service_blob GET /rails/active_storage/blobs/:signed_id/*filename(.:format) active_storage/blobs#show rails_blob_representation GET /rails/active_storage/representations/:signed_blob_id/:variation_key/*filename(.:format) active_storage/representations#show rails_disk_service GET /rails/active_storage/disk/:encoded_key/*filename(.:format) active_storage/disk#show update_rails_disk_service PUT /rails/active_storage/disk/:encoded_token(.:format) active_storage/disk#update rails_direct_uploads POST /rails/active_storage/direct_uploads(.:format) active_storage/direct_uploads#create

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

'rails', '~> 5.2.3'
ruby 2.6.3

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

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

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

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

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

unhappychoice

2019/09/04 04:29

UsersController はどうなっているでしょうか〜
sakana-suki

2019/09/04 05:24

見ていただきありがとうございます。 こんな感じになっています! ``` class UsersController < ApplicationController before_action :authenticate_user! def index @users = User.paginate(page: params[:page], per_page: 10) end def show @user = User.find_by(id: params[:id]) end end ```
guest

回答1

0

ベストアンサー

Userのインデックスページにアクセスしたいのであればusers/indexというパスではなく、/usersにアクセスしてください。
users/indexというパスでアクセスすることによってルーティングでのshowのパスであるusers/:idと誤認され、indexというidを持ったUserを探そうとしてしまうはずです。
おそらくコントローラーで@user = User.find_by(id: params[:id])と記述してあるため、この場合だと@user = User.find_by(id: 'index')というような形でUserを検索してしまい、@user = nilになってしまうために起こったエラーだと思われます。

投稿2019/09/04 04:47

haneru

総合スコア440

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

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

sakana-suki

2019/09/04 05:44

回答ありがとうございます!くじけそうになっていました。。 重ね重ね恐縮なのですが、 現在のルーティングだと、/users にアクセスするとルーティングエラーになってしまいます。 現在のroutes.rb が以下のようになっています。 ------------------------------------------------------------------------------ Rails.application.routes.draw do devise_for :users, :controllers => { :registrations => 'users/registrations', :sessions => 'users/sessions', :omniauth_callbacks => 'users/omniauth_callbacks' } devise_scope :user do get "sign_in", :to => "users/sessions#new" get "sign_out", :to => "users/sessions#destroy" end root 'posts#index' get 'users/:id', to: 'users#show', as: 'user' get 'users/index', to: 'users#index', as: 'user_index' resources :posts, only: %i(new create index show destroy) do resources :photos, only: %i(create) resources :likes, only: %i(create destroy) resources :comments, only: %i(create destroy) end end --------------------------------------------------------------------------------- この場合、 get 'users/index', to: 'users#index', as: 'user_index' を get 'users/', to: 'users#index', as: 'user_index' のように変更するのかな、行ごと削除かな、、、などといった改善案しか浮かばず(そしてうまくいきませんでした、、)、 再度詰まってしまいました。 haneru様の回答を読み、あっ!と思ったものの、うまく活かせません。どうしたらよいでしょうか
sakana-suki

2019/09/04 06:10

失礼しました。 サーバーを止めてもう一度起動したら、ちゃんと全ユーザーが表示されました! get 'users/index', to: 'users#index', as: 'user_index' を get 'users/', to: 'users#index', as: 'user_index' に変更で、よかったようです。 本当に助かりました!ありがとうございます これからも勉強していきたいと思います。
haneru

2019/09/04 07:24

今回の場合、showとindexだけですけど、今後、editやupdateなど増えてくると思います。 そのため、書き方としては`resources :users, only: %i(index show)`の方がいいかもしれません。
sakana-suki

2019/09/10 23:32

気づくのが遅れてしまい、返信が遅くなりました(汗) 確かにそちらの書き方のほうがすっきりとしていますね。 役立つ修正をありがとうございます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問