前提・実現したいこと
Ruby on Railsでユーザー同士のフォロー機能を作っています。
こちらのサイト等を参照しました。
Ruby on Rails ~フォロー(友達申請)機能の実装(コードメモ)
しかしフォローボタンが表示されません。フォロー一覧、フォロワー一覧は正常に表示されており、うまく機能しております。エラーメッセージは表示されておらず、単純にフォローボタンが表示されません。
ボタンについて試行錯誤するも先へ進めない状況です。
お見苦しい点がございましたら申し訳ありません。
お知恵を拝借頂けると幸いです。
該当のソースコード(show.html.erb)
ruby
1<div class="main user-show"> 2 <div class="container"> 3 <div class="user"> 4 <img src="<%= "/user_images/#{@user.image_name}" %>"> 5 <h2><%= @user.name %></h2> 6 <p><%= @user.email %></p> 7 <% if @user.id == @current_user.id %> 8 <%= link_to("編集", "/users/#{@user.id}/edit") %> 9 <% end %> 10 </div> 11 <ul class="user-tabs"> 12 <li class="active"><%= link_to("投稿", "/users/#{@user.id}") %></li> 13 <li><%= link_to("いいね!", "/users/#{@user.id}/likes") %></li> 14 </ul> 15 <% @user.posts.each do |post| %> 16 <div class="posts-index-item"> 17 <div class="post-left"> 18 <img src="<%= "/user_images/#{post.user.image_name}" %>"> 19 </div> 20 <div class="post-right"> 21 <div class="post-user-name"> 22 <%= link_to(post.user.name, "/users/#{post.user.id}") %> 23 </div> 24 <%= link_to(post.title, "/posts/#{post.id}") %> 25 </div> 26 </div> 27 <% end %> 28 <span id="user_<%= @user.id %>" class="follow-wrapper"> 29 <%= render 'follow_form', user: @user if signed_in? %> 30 </span> 31 <h4> 32 <%= render 'users/stats' %> 33 </h4> 34 </div> 35</div>
該当のソースコード(_follow_form.html.erb)
ruby
1<% unless current_user.id == user.id %> 2 <% if current_user.following?(user) %> 3 <%= render 'users/unfollow', user: user %> 4 <% else %> 5 <%= render 'users/follow', user: user %> 6 <% end %> 7<% end %>
該当のソースコード(_follow.html.erb)
ruby
1<%= form_for(current_user.following_relationships.build(following_id: user.id), 2 remote: true) do |f| %> 3 <div><%= f.hidden_field :following_id %></div> 4 <%= f.submit "Follow", class: "btn btn-large btn-primary" %> 5<% end %>
該当のソースコード(_unfollow.html.erb)
ruby
1<%= form_for(current_user.following_relationships.find_by(following_id: user.id), 2 html: { method: :delete }, 3 remote: true) do |f| %> 4 <%= f.submit "Unfollow", class: "btn btn-large" %> 5<% end %>
該当のソースコード(user.rb)
ruby
1class User < ApplicationRecord 2 # Include default devise modules. Others available are: 3 # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable 4 devise :database_authenticatable, :registerable, 5 :recoverable, :rememberable, :validatable 6 has_secure_password 7 8 validates :name, {presence: true} 9 validates :email, {presence: true, uniqueness: true} 10 11 def posts 12 return Post.where(user_id: self.id) 13 end 14 15 has_many :following_relationships, foreign_key: "follower_id", class_name: "Relationship", dependent: :destroy 16 has_many :followings, through: :following_relationships 17 18 has_many :follower_relationships, foreign_key: "following_id", class_name: "Relationship", dependent: :destroy 19 has_many :followers, through: :follower_relationships 20 21 def following?(other_user) 22 following_relationships.find_by(following_id: other_user.id) 23 end 24 25 def follow!(other_user) 26 following_relationships.create!(following_id: other_user.id) 27 end 28 29 def unfollow!(other_user) 30 following_relationships.find_by(following_id: other_user.id).destroy 31 end 32end
該当のソースコード(relationship.rb)
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
該当のソースコード(migration create_relationships)
ruby
1class CreateRelationships < ActiveRecord::Migration[5.2] 2 def change 3 create_table :relationships do |t| 4 t.integer :follower_id 5 t.integer :following_id 6 7 t.timestamps 8 end 9 add_index :relationships, :follower_id 10 add_index :relationships, :following_id 11 add_index :relationships, [:follower_id, :following_id], unique: true 12 end 13end
試したこと
deviseのインストールを、こちらを参考に4番の「rails g devise:install」まで行いました。
gem deviseをインストールする手順と理由
補足情報(FW/ツールのバージョンなど)
ruby 2.6.4p104
RubyGems 3.0.3
Rails 5.2.3
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/01/17 13:29
2020/01/17 13:46 編集
2020/01/18 03:53
2020/01/22 02:54 編集
2020/01/30 09:30