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

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

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

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

Q&A

解決済

1回答

241閲覧

Railsでフォロー機能を実装するとエラーが発生してしまう

rikuou

総合スコア25

Ruby on Rails

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

0グッド

1クリップ

投稿2020/07/15 09:30

railsでフォロー機能を実装したいために,以下のサイトを参考にして進めていきました

https://qiita.com/mitsumitsu1128/items/e41e2ff37f143db81897

#コード

#relationships マイグレーションファイル 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
#Userモデル class User < ApplicationRecord # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable mount_uploader :image_name, ImageUploader has_many :relationships has_many :followings, through: :relationships, source: :follow has_many :reverse_of_relationships, class_name: 'Relationships', foreign_key: 'follow_id' has_many :followers, through: :reverse_of_relationships, source: :user validates :name, presence: true validates :profile, length: { maximum: 200 } def posts return Post.where(user_id: self.id) end 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) if relationship relationship.destroy end end def following?(other_user) self.followings.include?(other_user) end end
#relationship モデル class Relationship < ApplicationRecord belongs_to :user belongs_to :follow, class_name: "User" validates :user_id, presence: true validates :follow_id, presence: true end
#relationships コントローラー class RelationshipsController < ApplicationController def create following = current_user.follow(@user) if following.save flash[:notice] = "ユーザーをフォローしました" redirect_to @user else flash[:notice] = "ユーザーのフォローに失敗しました" redirect_to @user end end def destroy following = current_user.unfollow(@user) if following.save flash[:notice] = "ユーザーのフォローを解除しました" redirect_to @user else flash[:notice] = "ユーザーのフォロー解除に失敗しました" redirect_to @user end end private def @user = User.find(params[:relationships][:follow_id]) end end
#app/views/relationships/_follow_button.html.erb <% unless current_user == user %> <% if current_user.following?(user) %> <%= form_for(current_user.relationships.find_by(follow_id: user.id), html: { method: :delete }) do |f| %> <%= hidden_field_tag :follow_id, user.id %> <%= f.submit 'Unfollow', class: 'btn btn-danger btn-block' %> <% end %> <% else %> <%= form_for(current_user.relationships.build) do |f| %> <%= hidden_field_tag :follow_id, user.id %> <%= f.submit 'Follow', class: 'btn btn-primary btn-block' %> <% end %> <% end %> <% end %>
#routes.rb Rails.application.routes.draw do devise_for :users, controllers: { registrarions: "users/registrations" } resources :users, only: [:show,:index] get '/' => "home#top" resources :posts resources :relationships, only: [:create, :destroy] # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html end

そしてユーザー詳細ページにフォローボタンを置こうと思い,
<%= render ‘relationships/follow_button’, user: @user %> をユーザー詳細ページに追加しました

#app/views/users/show.html.erb <div class="main show-box"> <div class="show-box-inner"> <div class="show-header"> <div class="show-header-item"> <div class="show-left"> <img src=<%= @user.image_name %> class="show-icon-image"> </div> <div class="show-right"> <div class="name-row"> <p class="show-user-name"><%= @user.name %></p> <div class="row-left"> <% if @user.id == current_user.id %> <%= link_to "プロフィールを編集", edit_user_registration_path, class: "edit-profile" %> <%= link_to "delete User", user_registration_path, {method: :delete, class: "delete-account"} %> <% end %> </div> </div> <p>プロフィール: <%= @user.profile %></p> </div> </div> </div> <%= render ‘relationships/follow_button’, user: @user %> #追加 <h2>投稿一覧</h2> <%= @user.posts.each do |post| %> <div class="posts-index-item"> <div class="post-left"> <img src=<%= post.user.image_name %> class="icon-image"> </div> <div class="post-right"> <%= link_to post.user.name, user_path(post.user.id), class: "change" %> <%= link_to post.content, edit_post_path(post.id), class: "content" %> </div> </div> <% end %> </div> </div>

その後ユーザー詳細ページにアクセスしようとすると,以下のエラーが起きてしまいます

#エラー内容

Showing /home/riku/self_application/Rails/Theidea/app/views/users/show.html.erb where line #23 raised: undefined local variable or method `‘relationships' for #<#<Class:0x00007f23c0297378>:0x000055731c804af0> Did you mean? relationships_url Extracted source (around line #23): 21 22 23 24 25 26 </div> <%= render ‘relationships/follow_button’, user: @user %> <h2>投稿一覧</h2> <%= @user.posts.each do |post| %> Rails.root: /home/riku/self_application/Rails/Theidea Application Trace | Framework Trace | Full Trace app/views/users/show.html.erb:23

relationshipsが定義されていないと出てしまいます.色々と調べたり考えてみたのですが自分の力だけだとわかりませんでした.
なぜこのエラー内容が生じてしまうのでしょうか.どなたかご享受お願いいたします.

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

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

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

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

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

guest

回答1

0

ベストアンサー

下記のようにシングルクオーテーション(ダブルクオーテーションでも大丈夫)で括れば、
エラーは解消できると思います。

ruby

1'relationships/follow_button'

投稿2020/07/15 09:49

Cojiro

総合スコア539

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

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

rikuou

2020/07/15 09:52

ありがとうございます!解決できました!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問