前提・実現したいこと
フォロー機能をつけたい
発生している問題・エラーメッセージ
該当のソースコード
relationshipsテーブル
class CreateRelationships < ActiveRecord::Migration[5.2] 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
【Model】
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
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 has_many :post_images, dependent: :destroy has_many :favorites, dependent: :destroy has_many :fav_post_images, through: :favorites, source: :post_image has_many :cosplay_favorites, dependent: :destroy has_many :cosplay_fav_post_images, through: :favorites, source: :post_image has_many :post_comments, dependent: :destroy validates :name, presence: true, length: { in: 2..15 } attachment :profile_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
post_image.rb
class PostImage < ApplicationRecord belongs_to :user has_many :favorites, dependent: :destroy has_many :fav_users, through: :favorites, source: :user has_many :cosplay_favorites, dependent: :destroy has_many :cosplay_fav_users, through: :cosplay_favorites, source: :user has_many :post_comments, dependent: :destroy attachment :real_image attachment :cosplay_image default_scope -> { order(created_at: :asc) } end
【Controller】
relationship_controller.rb
class RelationshipsController < ApplicationController before_action :set_user def create user = User.find(params[:relationship][:follow_id]) following = current_user.follow(user) if following.save flash[:success] = 'ユーザーをフォローしました' redirect_to user else flash.now[:alert] = 'ユーザーのフォローに失敗しました' redirect_to user end end def destroy user = User.find(params[:relationship][:follow_id]) 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
postimages_controller.rb
class PostImagesController < ApplicationController def new @post_image = PostImage.new end def create @post_image = PostImage.new(post_image_params) @post_image.user_id = current_user.id @post_image.save redirect_to post_images_path end def index @post_images = PostImage.page(params[:page]).reverse_order @post_image = PostImage.new @user = @post_image.user end def show @post_image = PostImage.find(params[:id]) @post_comment = PostComment.new end def destroy @post_image = PostImage.find(params[:id]) @post_image.destroy redirect_to post_images_path end private def post_image_params params.require(:post_image).permit(:real_image_name, :cosplay_image_name, :real_image, :cosplay_image, :caption, :favorites_count) end end
users_controller.rb
class UsersController < ApplicationController def show @user = User.find(params[:id]) @post_images = @user.post_images.page(params[:page]).reverse_order end def edit @user = User.find(params[:id]) end def update @user = User.find(params[:id]) @user.update(user_params) redirect_to user_path(@user.id) end private def user_params params.requrie(:user).permit(:name, :profile_image) end end
【部分テンプレート】
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 %>
【View】
index.html.erb
<div class="header"> <nav class="navigation"> <img src="/assets/logo.png"> <ul> <li> <%= link_to "ログアウト", destroy_user_session_path, method: :delete %> </li> <li> <%= link_to '投稿する', new_post_image_path %> </li> <li> <%= link_to 'マイページ', user_path(current_user.id) %> </li> </ul> </nav> </div> <div class="post_images_index_wrapper"> <% @post_images.each do |post_image| %> <div class="index_box"> <div class="post_images_index_user"> <ul> <li> <%= link_to user_path(post_image.id) do %> <%= attachment_image_tag @user, :profile_image, fallback: "no_image.jpg" %> <% end %> </li> <li> <p><%= link_to "#{post_image.user.name}", user_path(post_image.id) %></p> </li> <li> <%= render 'relationships/follow_button', user: @user %> </li> </ul> </div> <div class="post_images_index_title"> <div class="image_title"> <h2>Real</h2> </div> <div class="image_title"> <h2>Cosplay</h2> </div> </div> <div class="post_images_box"> <div class="post_image"> <%= attachment_image_tag post_image, :real_image %> </div> <div class="post_image"> <%= attachment_image_tag post_image, :cosplay_image %> </div> </div> <div class="image_name"> <%= post_image.real_image_name %> </div> <div class="image_name"> <%= post_image.cosplay_image_name %> </div> <div class="favorites_area"> <div class="favorite_area"> <%= render partial: 'post_images/post_images', locals: { post_image: post_image } %> </div> <div class="favorite_area"> <%= render partial: 'post_images/cosplay_post_images', locals: { post_image: post_image } %> </div> </div> <div class="image_caption"> <ul> <li> <%= link_to user_path(post_image.id) do %> <%= attachment_image_tag @user, :profile_image, fallback: "no_image.jpg" %> <% end %> </li> <li> <p><%= link_to "#{post_image.user.name}", user_path(post_image.id) %> </p> </li> <li> <p><%= link_to "#{post_image.post_comments.count}件のコメント", post_image_path(post_image.id) %></p> </li> </ul> <span class="caption"><%= post_image.caption %></span> </div> </div> <% end %> <%= paginate @post_images, class: "pagenate" %> </div>
【ルーティング】
routes.rb
Rails.application.routes.draw do devise_for :users root 'post_images#index' resources :post_images, only: [:new, :create, :index, :show, :destroy] do resource :post_comments, only: [:create, :destroy] end resources :users, only: [:show, :edit] post '/favorite/:id' => 'favorites#favorite', as: 'favorite' post '/cosplay_favorite/:id' => 'cosplay_favorites#favorite', as: 'cosplay_favorite' resources :relationships, only: [:create, :destroy] # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html end
補足情報(FW/ツールのバージョンなど)
ruby 2.5.7p206
Rails 5.2.4.1
よろしくお願いいたします。
回答1件
あなたの回答
tips
プレビュー