質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

解決済

1回答

488閲覧

rails いいね機能が実装できません。

naoya0922

総合スコア23

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

0クリップ

投稿2020/12/27 01:00

編集2020/12/27 01:02

Twitterクローンの投稿にいいね機能を実装させたいのですが、以下のuserモデルのselfの部分で「そんなメソッドはない」とエラーが出てしまったのですが、selfにはcurrent_userが入るようにしているつもりです。

#userモデル class User < ApplicationRecord before_save { self.email.downcase! } validates :name, presence: true, length: { maximum: 50 } validates :email, presence: true, length: { maximum: 255 }, format: { with: /\A[\w+\-.]+@[a-z\d\-.]+.[a-z]+\z/i }, uniqueness: { case_sensitive: false } has_secure_password has_many :microposts has_many :relationships has_many :followings, through: :relationships, source: :follow has_many :reverses_of_relationship, class_name: 'Relationship', foreign_key: 'follow_id' has_many :followers, through: :reverses_of_relationship, source: :user has_many :favorites has_many :favorites, through: :favorites, source: :micropost 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 def feed_microposts Micropost.where(user_id: self.following_ids + [self.id]) end #いいね機能↓ =begin def like(micropost) self.favorites.find_or_create_by(micropost_id: micropost.id) end def unlike(micropost) favorite = self.favorites.find_by(micropost_id: micropost.id) favorite.destroy if favorite end =end def already_favorite?(micropost)  self.favorites.exists?(micropost_id: micropost.id) end end
# favoriteコントローラー class FavoritesController < ApplicationController before_action :require_user_logged_in def create @favorite = current_user.favorites.create(micropost_id :params[:micropost_id]) flash[:success] = 'いいねしました。' redirect_back(fallback_location: root_path) end =begin user = User.find(params[:micropostw_id]) current_user.like(micropost) flash[:success] = 'いいねしました。' redirect_to user # redirect_back(fallback_location: root_path) =end def destroy @micropost = Micropost.find(params[:micropost_id ]) @favorite = current_user.favorites.find_by(micropost_id :@micropost) @favorite.destroy flash[:success] = 'いいねを外しました。' redirect_back(fallback_location: root_path) =begin user = User.find(params[:micropost_id]) current_user.unlike(micropost) flash[:success] = 'いいねを外しました。' redirect_to user # redirect_back(fallback_location: root_path) =end end end

パーシャルとして違うファイルで呼び出す。

<% if current_user.already_favorite?(micropost) %>
<%= link_to 'いいねを外す', favorite_path(micropost),method: :delete %>
<% else %>
<%= link_to 'いいねする', favorites_path, method: :post %>

<% end %>

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

user = User.find(params[:micropostw_id])

user = User.find(params[:micropost_id])
に直すとどうなりますか?(wがタイプミス?)

また、それを対処する市内にかかわらず、paramsに入っている値が期待した値になっているかどうか確認した方がよさそうです。

投稿2020/12/27 01:15

siruku6

総合スコア1382

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

naoya0922

2020/12/27 01:57

お返事ありがとうございます。 その確認方法はどのような方法がありますか。 初心者なので知識が少なくてすみません。
siruku6

2020/12/27 02:40

>user = User.find(params[:micropostw_id]) >を >user = User.find(params[:micropost_id]) >に直すとどうなりますか?(wがタイプミス?) この結果はいかがでしたか?エラー文言に変化はありましたか?? ------ >その確認方法はどのような方法がありますか。 Gemfileの development, test グループに、pry-rails と pry-byebug というgemを入れます。 そうすると、binding.pryというメソッドを書いた場所にブレークポイントが設置できるようになります。 使い方の概要はここで確認できますので、試してみましょう。今後も重宝すると思います。 【pry-byebugでrubyをデバッグする】 https://qiita.com/AknYk416/items/6f0bec58712edaf4940e
naoya0922

2020/12/27 03:46

ありがとうございます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問