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

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

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

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

Q&A

解決済

1回答

1016閲覧

undefined method `[]' for nil:NilClass

kawakun-----

総合スコア13

Ruby

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

0グッド

0クリップ

投稿2021/03/16 07:38

データベース、モデル等
確認しましたが、nillになる原因がわかりません。。。。

どうかご教授お願いいたします。。。。

relationshipsコントローラー

class RelationshipsController < ApplicationController before_action :set_user def create following = current_user.follow(@user) if following.save flash[:success] = 'ユーザーをフォローしました' redirect_to @user else flash.now[:alert] = 'ユーザーのフォローに失敗しました' redirect_to @user end end def destroy following = current_user.unfollow(@user) if following.destroy flash[:success] = 'ユーザーのフォローを解除しました' redirect_to @user else flash.now[:alert] = 'ユーザーのフォロー解除に失敗しました' redirect_to @user end end private def set_user @user = User.find(params[:relationship][:follow_id]) end end

relationshipsモデル

class Relationship < ApplicationRecord belongs_to :user belongs_to :follow, class_name: 'User' validates :user_id, presence: true validates :follow_id, presence: true end

userモデル

class User < ApplicationRecord # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable with_options presence: true do validates :name validates :birth_day validates :gender validates :hobby validates :self_introduction, length: { maximum: 200 } validates :image end enum gender: { man: 0, woman: 1} has_one_attached :image has_many :relationships has_many :followings, through: :relationships, source: :follow has_many :reverse_of_relationships, class_name: 'Relationship', foreign_key: 'follow_id' has_many :followers, through: :reverse_of_relationships, source: :user def follow(other_user) unless self == other_user self.relationships.find_or_create_by(follow_id: other_user.id) end end def unfollow(other_user) relationship = self.relationships.find_by(follow_id: other_user.id) relationship.destroy if relationship end def following?(other_user) self.followings.include?(other_user) end end

データベース

class CreateRelationships < ActiveRecord::Migration[6.0] def change create_table :relationships do |t| t.references :user, foreign_key: true t.references :follow, foreign_key: { to_table: :users } t.timestamps t.index [:user_id, :follow_id], unique: true end end end

toppage ビュー

<div class="jumbotron"> <div class="container"> <nav class="navbar navbar-light"> <h1>マッチングを探す</h1> </div> <div class="top-authentication text-center"> <% if user_signed_in? %> <div class="top-authentication__sign-in-btn m-3"> <%= link_to "ログアウト", destroy_user_session_path, method: :delete, class: "logout" %> </div> <%= link_to "マイページ", user_path(current_user.id) %> <% else %> <div class="top-authentication__sign-up-btn m-3"> <%= link_to "アカウントを作成する", new_user_registration_path, class: "sign-up" %> </div> <div class="top-authentication__sign-in-btn m-3"> <%= link_to "ログイン", new_user_session_path, class: "login" %> </div> <% end %> <% @users.each do |user|%> <%= link_to user_path (user.id) do %> <div class='user-img-content'> <%= image_tag user.image, class: "user-img" %> </div> <% end %> <%= render "relationships/follow_button.html.erb" %> <% end %>

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

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

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

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

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

maisumakun

2021/03/16 07:41

どの行についてエラーが出たのですか?
kawakun-----

2021/03/16 07:42

失礼しました。。。 こちらです。 private def set_user @user = User.find(params[:relationship][:follow_id]) end end
ozwk

2021/03/16 07:49

その記事のコメント欄にいろいろ書いてあります。
kawakun-----

2021/03/16 07:50

かしこまりました! 再度、確認してみます!
退会済みユーザー

退会済みユーザー

2021/03/16 07:52

teratailでも2年前に同じタイトルで質問がついていて解決してるっぽいので、 回答にURL貼っておきました。ご一読いただけましたら幸いです。
kawakun-----

2021/03/16 08:01

ありがとうございます!!
guest

回答1

0

ベストアンサー

たぶんなんだけど、さっきググったときに先に見つけたこれかな?
同じ記事で作成してそうな気がする。

参考URL teratail NoMethodError (undefined method `[]' for nil:NilClass)
https://teratail.com/questions/231324

投稿2021/03/16 07:50

退会済みユーザー

退会済みユーザー

総合スコア0

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

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

kawakun-----

2021/03/16 07:52

ありがとうございます!! 確認してみます!!
kawakun-----

2021/03/16 08:00

https://teratail.com/questions/231324 こちら参考にして、修正してみました!! <% unless current_user == @user %> <% if current_user.following?(@user) %> <%= form_for(current_user.relationships.find_by(follow_id: @user), html: { method: :delete }) do |f| %> <%= hidden_field_tag :follow_id, @user %> <%= f.submit 'Unfollow', class: 'btn btn-danger btn-block' %> <% end %> <% else %> <%= form_for(current_user.relationships.build) do |f| %> <%= params[:follow_id] @user %> <%= f.submit 'Follow', class: 'btn btn-primary btn-block' %> <% end %> <% end %> <% end %> 回答のonlyの部分が理解できないのですが、 ご教授願えますでしょうか。。。 何度もすみません、、、
退会済みユーザー

退会済みユーザー

2021/03/16 08:16 編集

なんか恐ろしく勘違いされてしまっていると思うのですが、 僕はググるのがちょっと上手いだけのPHPerであり、RubyはおろかRailsも扱ったことないです。 Rubyできる人に聞いた方が確実だと思いますよー、といいつつ…以下のURLが参考になるかと。 Qiita 基礎Ruby on Rails Chapter8 https://qiita.com/tseno/items/46065bc132b189c37ccb このページの中をF検索とかで「ルーティングの設定」この項目を探してください。 上手くパラメータが取れないページではエラーを吐いてしまう為、 メソッドを参照するURLを限定する…みたいな話だと思うのですが、 PHPerなのでRubyやRailsは全くの初見ですw 見当はずれのことを教えている場合は他の方がツッコミを入れてくれると思うので、 突っ込まれるまでは大丈夫かなぁ? とりあえず、Qiitaの記事が合っていて、疑問の解消に役立ってくれることを祈りますっ
kawakun-----

2021/03/16 08:21

かしこまりました!! ありがとうございます!! 記事のおかげで理解できました。 ありがとうございます。。。。。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問