やりたいこと
RailsTutorialを参考にしてフォロー機能のAjaxで実装しようとしています。
現在の状態は非同期ではフォローされないものの、ページ遷移をするわけでもなく、ページをリロードすればフォローされるというような状況です。
Ruby
1class User < ApplicationRecord 2 mount_uploader :avatar_image, AvatarImagesUploader 3 validates :nick_name, presence: true 4 # Include default devise modules. Others available are: 5 # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable 6 devise :database_authenticatable, :registerable, 7 :recoverable, :rememberable, :validatable, :omniauthable, omniauth_providers: [:twitter] 8 has_many :active_relationships, class_name: "Relationship", 9 foreign_key: "follower_id", 10 dependent: :destroy 11 has_many :passive_relationships, class_name: "Relationship", 12 foreign_key: "followed_id", 13 dependent: :destroy 14 has_many :following, through: :active_relationships, source: :followed 15 has_many :followers, through: :passive_relationships, source: :follower 16 17 18 def self.from_omniauth(auth) 19 find_or_create_by(provider: auth['provider'], uid: auth['uid']) do |user| 20 user.provider = auth['provider'] 21 user.uid = auth['uid'] 22 user.username = auth['info']['nickname'] 23 end 24 end 25 26 def self.new_with_session(params, session) 27 if session['devise.user_attributes'] 28 new(session['devise.user_attributes']) do |user| 29 user.attributes = params 30 end 31 else 32 super 33 end 34 end 35 36 def follow(other_user) 37 active_relationships.create(followed_id: other_user.id) 38 end 39 40 def unfollow(other_user) 41 active_relationships.find_by(followed_id: other_user.id).destroy 42 end 43 44 def following?(other_user) 45 following.include?(other_user) 46 end 47end
Ruby
1class Relationship < ApplicationRecord 2 belongs_to :follower, class_name: "User" 3 belongs_to :followed, class_name: "User" 4 validates :follower_id, presence: true 5 validates :followed_id, presence: true 6end
Ruby
1class UsersController < ApplicationController 2 def index 3 @search = User.ransack(params[:q]) 4 @result = @search.result(distinct: true) 5 @check = params[:q] 6 end 7 8 def show 9 end 10 11 def following 12 @users = current_user.following 13 render "show_follow" 14 end 15 16 def followers 17 @users = current_user.followers 18 render "show_follow" 19 end 20end
Ruby
1class RelationshipsController < ApplicationController 2 def create 3 @user = User.find(params[:followed_id]) 4 current_user.follow(@user) 5 respond_to do |format| 6 format.html {redirect_to @user} 7 format.js 8 end 9 end 10 11 def destroy 12 @user = Relationship.find(params[:id]).followed 13 current_user.unfollow(@user) 14 respond_to do |format| 15 format.html {redirect_to @user} 16 format.js 17 end 18 end 19end
Ruby
1<% @users.each do |users| %> 2 <div class="d-inline-flex p-2 bd-highlight col-12 border"> 3 <p><%= image_tag users.avatar_image.thumb.url,:size =>'50x50' %></p> 4 <p class="pl-3 col-4 d-block"><%= users.nick_name.truncate(8) %></p> 5 <div class="d-flex flex-row justify-content-end"> 6 <% if current_user != users %> <!-- フォローフォーム開始 --> 7 <div id="show_follow_form"> 8 <% if current_user.following?(users) %> 9 <%= form_for(current_user.active_relationships.find_by(followed_id: @users.id), 10 html: { method: :delete }, remote: true) do |f| %> 11 <%= f.submit "フォロー中", class: "btn mt-3" %> 12 <% end %> 13 <% else %> 14 <%= form_for(current_user.active_relationships.build, remote: true) do |f| %> 15 <div><%= hidden_field_tag :followed_id, @users.id %></div> 16 <%= f.submit "フォロー", class: "btn btn-primary ml-3 mt-3" %> 17 <% end %> <!-- フォローフォーム終了 --> 18 <% end %> 19 </div> 20 <p><%= link_to "送る", "#", class: "btn btn-info m-3" %></p> 21 <% end %> 22 </div> 23 </div> 24<% end %>
Ruby
1_show_follow.html.erb 2 3<%= form_for(current_user.active_relationships.build, remote: true) do |f| %> 4 <div><%= hidden_field_tag :followed_id, @users.id %></div> 5 <%= f.submit "フォロー", class: "btn btn-primary ml-3 mt-3" %> 6<% end %> 7 8_show_unfollow.html.erb 9 10<%= form_for(current_user.active_relationships.find_by(followed_id: @users.id), 11 html: { method: :delete }, remote: true) do |f| %> 12 <%= f.submit "フォロー中", class: "btn mt-3" %> 13<% end %>
Ruby
1create.js.erb 2 3$("#show_follow_form").html("<%= escape_javascript(render('users/show_follow')) %>"); 4$("#index_follow_form").html("<%= escape_javascript(render('users/index_follow')) %>"); 5 6destroy.js.erb 7 8$("#show_follow_form").html("<%= escape_javascript(render('users/show_unfollow')) %>"); 9$("#index_follow_form").html("<%= escape_javascript(render('users/index_unfollow')) %>");
出ているエラー
Rendering relationships/create.js.erb Rendered users/_show_follow.html.erb (2.3ms) Rendered relationships/create.js.erb (3.7ms) Completed 500 Internal Server Error in 64ms (ActiveRecord: 30.5ms) ActionView::Template::Error (undefined method `id' for nil:NilClass): 1: <%= form_for(current_user.active_relationships.build, remote: true) do |f| %> 2: <div><%= hidden_field_tag :followed_id, @users.id %></div> 3: <%= f.submit "フォロー", class: "btn btn-primary ml-3 mt-3" %> 4: <% end %> app/views/users/_show_follow.html.erb:2:in `block in _app_views_users__show_follow_html_erb__2645418965663559337_70272209315780'
わからないこと
「undefined method `id' for nil:NilClass」と出ていてもUsersテーブルにもRelationshipsテーブルにもidカラムがあり値はちゃんと入っているのになぜNilになるのかがわかりません。
どなたかご教授願います。。。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/02/11 12:35 編集
2020/02/11 12:42
2020/02/11 12:47