初学者の者で、現在Qiitaの下記の記事を参考にさせて頂きながらフォロー機能の実装を試みております。
Qiita:railsでフォロー機能をつける。
下記のようなエラーが生じて解決法が見出せていないために、今回ご質問させていただきます。
以下、詳細を記述いたします。
環境
ruby 2.5.3
rails 5.2.3
###事象
特定userの詳細ページにて「フォローする」ボタンを表示させようとして当該ページ(http://localhost:3000/users/2)にアクセスしたところ、下記のエラーが発生しました。
error
1undefined method `relationships_path' for #<#<Class:0x00007fa354d636a8>:0x00007fa354d61c18>
- 対象のViewファイル
現在のログインユーザーであるcurrent_user(Gem deviseにより定義)について、後述のfollowing_relationshipsのテーブルを生成しようとしています。
erb
1<%= form_for current_user.following_relationships.new(following_id: @user.id) do |f| %> 2 <div><%= f.hidden_field :following_id %></div> 3 <%= f.submit "フォローする", class: "btn btn-sm btn-primary follow-btn" %> 4<% end %>
- Model
上記内でのメソッドfollowing_relationshipsの定義元はUserモデル(app/models/user.rb)内にあります。
以下、フォロー機能周りの記述の抜粋
ruby
1class User < ApplicationRecord 2 3 has_many :following_relationships, foreign_key: "follower_id", 4 class_name: "Relationship", dependent: :destroy 5 6 has_many :followings, through: :following_relationships 7 8 has_many :follower_relationships, foreign_key: "following_id", 9 class_name: "Relationship", dependent: :destroy 10 11 has_many :followers, through: :follower_relationships 12 13 # ユーザー登録機能はGem deviseを使用 14 devise :database_authenticatable, :registerable, 15 :recoverable, :rememberable,:trackable, :validatable 16 17 def following?(other_user) 18 following_relationships.find_by(following_id: other_user.id) 19 end 20 21 # 次行のfollow!は後述のCreateアクション内で使用 22 def follow!(other_user) 23 following_relationships.create!(following_id: other_user.id) 24 end 25 26 def unfollow?(other_user) 27 following_relationships.find_by(following_id: other_user.id).destroy 28 end 29end 30
Userモデルに依存するRelationshipモデルは下記であり、モデル間の関連付けやカラムの整合は取れているように思います。
ruby
1class Relationship < ApplicationRecord 2 belongs_to :follower, class_name: "User" 3 belongs_to :following, class_name: "User" 4 validates :follower_id, presence: true 5 validates :following_id, presence: true 6end
また、relationshipsテーブルの構成は、db/schema.rbより
ruby
1 create_table "relationships", force: :cascade do |t| 2 t.integer "follower_id" 3 t.integer "following_id" 4 t.datetime "created_at", null: false 5 t.datetime "updated_at", null: false 6 t.index ["follower_id", "following_id"], name: "index_relationships_on_follower_id_and_following_id", unique: true 7 t.index ["follower_id"], name: "index_relationships_on_follower_id" 8 t.index ["following_id"], name: "index_relationships_on_following_id" 9 end
- Controller
以下は、狙いのcreateアクションについての記述です。
ruby
1class RelationshipsController < ApplicationController 2 def create 3 @user = User.find(params[:relationship][:following_id]) 4 current_user.follow!(@user) 5 if following_relationships.save 6 flash[:notice] = "フォローしました!" 7 redirect_back(fallback_location: root_path) 8 else 9 flash[:notice] = "フォローに失敗しました。" 10 redirect_back(fallback_location: root_path) 11 end 12 end 13end 14
- Routing
routes.rbのRelationshipsについての記述部分です。
ruby
1Rails.application.routes.draw do 2 3 resources :users do 4 member do 5 get :following, :followers 6 end 7 resources :relationships, only: [:create, :destroy] 8 end 9 10end
上記によりPATHは以下のとおり生成されています。
following_user GET /users/:id/following(.:format) users#following followers_user GET /users/:id/followers(.:format) users#followers user_relationships POST /users/:user_id/relationships(.:format) relationships#create user_relationship DELETE /users/:user_id/relationships/:id(.:format) relationships#destroy
以上から、Viewから関係性を遡っていき、following_relationshipsメソッドに紐づく各モデル間の関連付けやカラムの整合、ルーティングの生成は出来ているように思います。
静的コード解析ツールのrubocopでもチェックしましたが、全角文字混入の指摘なども確認されなかったので、なぜパスが通らずにうまくいかないのか頭を悩ませております。
根本的な基礎事項の理解が至らずに生じた些細なエラーなのかもしれませんが、form_forの使用法や、既存の質疑などの情報を参照しても解決法が見つかっていないために、ここでご相談させて頂きます。
お手数をお掛けいたしますが、ご助言やご指摘のほど宜しくお願い申し上げます。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/06/30 04:37