前提
Railsでいいね機能を実装しています。
いいね機能を実装中にログアウトボタンを押したら下記のエラーメッセージが発生しました。
実現したいこと
- いいね機能の実装。
- 正常にログイン、ログアウト出来るようにすること。
発生している問題・エラーメッセージ
エラーメッセージ
NoMethodError in Posts#index Showing /Users/********/projects/********/app/views/likes/_like.html.erb where line #1 raised: undefined method `id' for nil:NilClass def liked_by?(user) likes.where(user_id: user.id).exists? end with_options presence: true do
該当ソースコード
config/routes.rb
1Rails.application.routes.draw do 2 devise_for :users, controllers: { registrations: 'users/registrations' } 3 root 'posts#index' 4 resources :posts do 5 resources :comments, only: [:create, :destroy] 6 resource :likes, only: [:create, :destroy] 7 end 8 resources :users, only: :show 9end 10
app/models/like.rb
1class Like < ApplicationRecord 2 belongs_to :user 3 belongs_to :post 4 validates_uniqueness_of :post_id, scope: :user_id 5end
app/models/post.rb
1class Post < ApplicationRecord 2 belongs_to :user, optional: true 3 has_many :comments, dependent: :destroy 4 has_one_attached :image 5 has_many :likes, dependent: :destroy 6 has_many :users, through: :likes 7 8 def liked_by?(user) 9 likes.where(user_id: user_id).exists? 10 end 11 12 with_options presence: true do 13 validates :subject 14 validates :course 15 validates :unit 16 validates :teaching_materials 17 end 18 19 validates :introduction, length: { maximum: 300 } 20 validates :development, length: { maximum: 1000 } 21 validates :summary, length: { maximum: 300 } 22 23 with_options numericality: { only_integer: true, less_than_or_equal_to: 50 } do 24 validates :introduction_time 25 validates :development_time 26 validates :summary_time 27 end 28end
app/models/user.rb
1class User < ApplicationRecord 2 devise :database_authenticatable, :registerable, 3 :recoverable, :rememberable, :validatable 4 5 has_many :posts, dependent: :destroy 6 has_many :comments, dependent: :destroy 7 has_many :likes, dependent: :destroy 8 has_many :posts, through: :likes 9 10 with_options presence: true do 11 validates :nickname 12 validates :password_confirmation, format: { with: /\A(?=.*?[a-z])(?=.*?\d)[a-z\d]+\z/i, message: 'には英字と数字の両方を含めて設定してください' }, 13 if: :password_required? 14 validates :last_name, format: { with: /\A[ぁ-んァ-ヶ一-龥々ー]+\z/, message: 'には全角文字を使用してください' } 15 validates :first_name, format: { with: /\A[ぁ-んァ-ヶ一-龥々ー]+\z/, message: 'には全角文字を使用してください' } 16 validates :last_name_kana, format: { with: /\A[ァ-ヶー-]+\z/, message: 'には全角カナを使用してください' } 17 validates :first_name_kana, format: { with: /\A[ァ-ヶー-]+\z/, message: 'には全角カナ文字を使用してください' } 18 end 19 20 def update_without_current_password(params, *options) 21 params.delete(:current_password) 22 23 if params[:password].blank? && params[:password_confirmation].blank? 24 params.delete(:password) 25 params.delete(:password_confirmation) 26 end 27 28 result = update(params, *options) 29 clean_up_passwords 30 result 31 end 32 33 def soft_delete 34 update_attribute(:deleted_at, Time.current) 35 end 36 37 def active_for_authentication? 38 super && !deleted_at 39 end 40 41 def inactive_message 42 !deleted_at ? super : :deleted_account 43 end 44end
app/views/likes/_like.html.erb
1<% if post.liked_by?(current_user) %> 2 <%= link_to(post_likes_path(post), method: :delete, remote: true, id: :"like-button-#{post.id}") do %> 3 <i class="fa-lg fas fa-heart icon-btn liked"></i> 4 <%= post.likes.count %> いいね 5 <% end %> 6<% else %> 7 <%= link_to(post_likes_path(post), method: :post, remote: true, id: :"like-button-#{post.id}") do %> 8 <i class="fa-lg fas fa-heart icon-btn not-like"></i> 9 <%= post.likes.count %> いいね 10 <% end %> 11<% end %>
試したこと
def liked_by?(user) likes.where(user_id: user.id).exists? end
の2行目を
likes.where(user_id: @user).exists?
にすることでログインはできたのですが、いいね機能が正常に実装されなくなります。
お力添えいただきたいです。どうかよろしくお願いいたします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2022/05/10 00:18
退会済みユーザー
2022/05/10 01:15