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

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

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

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

Q&A

1回答

1502閲覧

Railsでフォロー機能を作ろうとする時にエラーが生じてしまう

rikuou

総合スコア25

Ruby on Rails

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

0グッド

0クリップ

投稿2020/07/15 23:34

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.rb 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: 'Relationship', 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.rb class Relationship < ApplicationRecord belongs_to :user belongs_to :follow, class_name: "User" validates :user_id, presence: true validates :follow_id, presence: true end
#relationships_controller.rb 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 @user = User.find(params[:relationship][: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>

その後ユーザー1でログインしているときにユーザー2の詳細ページ user.showにアクセスすると以下のエラーが生じてしまいます

#エラー内容

ActiveRecord::StatementInvalid in Users#show Showing /home/riku/self_application/Rails/Theidea/app/views/relationships/_follow_button.html.erb where line #2 raised: SQLite3::SQLException: no such column: relationships.user_id Extracted source (around line #36): 34 35 36 37 38 39 def following?(other_user) self.followings.include?(other_user) end end Trace of template inclusion: #<ActionView::Template app/views/users/show.html.erb locals=[]> Rails.root: /home/riku/self_application/Rails/Theidea Application Trace | Framework Trace | Full Trace app/models/user.rb:36:in `following?' app/views/relationships/_follow_button.html.erb:2 app/views/users/show.html.erb:23

following?メソッドに問題があると書かれていますがどこが問題があるのかわかりません...
どなたかご享受お願いします

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

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

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

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

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

guest

回答1

0

SQLite3::SQLException: no such column: relationships.user_id

「relationshipsテーブルに、user_idカラムがないよ」と出ているので、まずは

  • relationshipsテーブルが作られているか

  • user_id という名前のカラムがあるか

を確認してみると良いのではないかと思います。

投稿2020/07/17 03:16

kyoruni

総合スコア93

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問