フォロー機能実装後にフォロー解除ができない

実現したいこと

フォロー解除をしたい。

前提

フォロー機能の理解のためオリジナルアプリの予約ページ(本来はいらないのですが)のユーザーをフォローできるように実装しました。

フォローはできるのですが、フォローの解除ができません。

エラーメッセージも出ていないのでどこが問題なのかわからずにいます。
フォロー中の箇所を押すと、ブラウザのサイト名が一瞬点滅するだけで何も起きません。VScodeの<title>のところです。

コントローラーではなく、ルーティングの設定でミスがあるのかなとも思って色々と試しましたが解決しませんでした。。。

同様のエラーにあった方がおられましたらご教示よろしくお願いいたします。

Image from Gyazo

routes.rbのソースコード

Rails.application.routes.draw do
devise_for :users
resources :users, only: [:show, :edit, :update]
root to: 'requests#index'
resources :requests do
resources :comments, only: [:create, :destroy]
collection do
get 'search'
end
resource :likes, only: [:create, :destroy]
resources :users do
resource :relationships, only: [:create, :destroy]
end

end
resources :questions, only: [:index, :show]

end

relationships.controllerのソースコード

class RelationshipsController < ApplicationController
before_action :request_go

def create
follow.save
redirect_to request_path(comment.request)
end

def destroy
follow.destroy
redirect_to root_path
end

private
def request_go
@request = Request.find(params[:id])
@comments = @request.comments.includes(:user)
@user = User.find(params[:id])
follow = current_user.active_relationships.new(follower_id: params[:user_id])

end

end

relationshipモデルのソースコード

class Relationship < ApplicationRecord
belongs_to :following, class_name: "User"
belongs_to :follower, class_name: "User"
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 :nickname validates :sir_name validates :first_name validates :phone_number validates :address end validates :nickname, length: { maximum: 6 } validates :password, format: { with: /\A(?=.*[a-zA-Z])(?=.*\d)[a-z\d]+\z/i }

has_many :requests
has_many :comments
has_many :likes, dependent: :destroy

has_many :active_relationships, class_name: "Relationship", foreign_key: :following_id
has_many :followings, through: :active_relationships, source: :follower

has_many :passive_relationships, class_name: "Relationship", foreign_key: :follower_id
has_many :followers, through: :passive_relationships, source: :following

def followed_by?(user)
follower = passive_relationships.find_by(following_id: user.id)
return follower.present?
end

end

requestモデルのソースコード

class Request < ApplicationRecord
extend ActiveHash::Associations::ActiveRecordExtensions
belongs_to :category

validates :title, presence: true
validates :date, presence: true
validates :description, presence: true
validates :expected_length, presence: true
validates :expected_place, presence: true
validates :category_id, numericality: { other_than: 1, message: "can't be blank" }
validates :images, length: { minimum: 1, maximum: 5, message: "は1枚以上5枚以下にしてください" }
validate :validate_images_presence

belongs_to :user
has_many :comments, dependent: :destroy
has_one_attached :image
has_many_attached :images
has_many :likes, dependent: :destroy

def self.search(search)
if search != ''
Request.where('title LIKE ? OR description LIKE ?', "%#{search}%", "%#{search}%")
else
Request.all
end
end

def liked_by?(user)
likes.where(user_id: user.id).exists?
end

private

def validate_images_presence
return if images.attached?
errors.delete(:images)
end

end

試したこと

routes.rbでresources :requestsにルーティングネストする形でresource :relationships, only: [:create, :destroy]を入れました。
フォローするを押した場合のレコードはちゃんと保存できておりました。

コメントを投稿

0 コメント