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

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

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

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

Q&A

解決済

1回答

810閲覧

rails でいいね機能を作りたい

naoya0922

総合スコア23

Ruby on Rails

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

0グッド

0クリップ

投稿2020/12/28 01:07

編集2020/12/30 04:56

rails 初心者です。
問題: def create
user = Favorite.find(params[:micropost_id])で:
[Couldn't find Favorite with 'id'=micropost_id]
とエラーが発生してしまうのですが、
①どうすればmicropost_idを見つけることができるでしょうか?

②userモデルに書いたlike,unlike,already_favorite? めそっどは正しく書けているでしょうか?
↓ではuser モデルにメソッドを書いてそれをfavortiteコントローラーで使うようにしています。

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 :favorites, dependent: :destroy has_many :fav_microposts, through: :favorites, source: :micropost def like(other_user) favorites.find_or_create_by(micropost_id :other_user.id) end def unlike(other_user) favorite = self.favorites.find_by(micropost_id: other_user.id) favorite.destroy if favorite end def already_favorite?(other_user) self.favorites.exists?(other_user) end end

下は#favorite_controller

class FavoritesController < ApplicationController def create user = Favorite.find(params[:micropost_id]) current_user.like(user) flash[:success] = 'いいねをしました。' redirect_to user end def destroy user = Favorite.find(params[:micropost_id]) current_user.unlike(user) flash[:success] = 'いいねを外しました。' redirect_to user end end

routes↓

Rails.application.routes.draw do root to: 'toppages#index' get 'login', to: 'sessions#new' post 'login', to: 'sessions#create' delete 'logout', to: 'sessions#destroy' get 'signup', to: 'users#new' resources :users, only: [:index, :show, :new, :create] do member do get :followings get :followers end end resources :microposts, only: [:create, :destroy] post "favorites/:micropost_id/create" => "favorites#create" post "favorites/:micropost_id/destroy" => "favorites#destroy" end

schema.rb

ActiveRecord::Schema.define(version: 2020_12_27_113344) do create_table "favorites", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| t.bigint "user_id" t.bigint "micropost_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.index ["micropost_id"], name: "index_favorites_on_micropost_id" t.index ["user_id", "micropost_id"], name: "index_favorites_on_user_id_and_micropost_id", unique: true t.index ["user_id"], name: "index_favorites_on_user_id" end create_table "microposts", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| t.string "content" t.bigint "user_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.index ["user_id"], name: "index_microposts_on_user_id" end create_table "relationships", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| t.bigint "user_id" t.bigint "follow_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.index ["follow_id"], name: "index_relationships_on_follow_id" t.index ["user_id", "follow_id"], name: "index_relationships_on_user_id_and_follow_id", unique: true t.index ["user_id"], name: "index_relationships_on_user_id" end create_table "users", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| t.string "name" t.string "email" t.string "password_digest" t.datetime "created_at", null: false t.datetime "updated_at", null: false end add_foreign_key "favorites", "users" add_foreign_key "favorites", "users", column: "micropost_id" add_foreign_key "microposts", "users" add_foreign_key "relationships", "users" add_foreign_key "relationships", "users", column: "follow_id" end

#パーシャルファイル(いいねリンク)

<% microposts.each do |micropost| %> <li class="media mb-3"> <img class="mr-2 rounded" src="<%= gravatar_url(micropost.user, { size: 50 }) %>" alt=""> <div class="media-body"> <div> <%= link_to micropost.user.name, user_path(micropost.user) %> <span class="text-muted">posted at <%= micropost.created_at %></span> </div> <div> <p><%= micropost.content %></p> </div> <div> <% if current_user == micropost.user %> <%= link_to "Delete", micropost, method: :delete, data: { confirm: "You sure?" }, class: 'btn btn-danger btn-sm' %> <% end %> <% if current_user.already_favorite?(micropost) %>  <%= link_to 'いいねを外す', "/favorites/#{:micropost_id}/create", method: :delete %> <% else %> <%= link_to 'いいね', "/favorites/#{:micropost_id}/create" , method: :post %> <% end %>

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

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

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

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

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

nasuk47

2020/12/30 03:51

パーシャルファイルがマークダウン適用されてません。 修正をお願いします。 createアクションの初めにbinding.pryで止めてparamsの値を確認できますでしょうか?
naoya0922

2020/12/30 05:12

お返事ありがとうございます。 user = Favorite.find(params[:micropost_id]) current_user.like(user) の部分を以下のように変更したらうまくいきました。 @favorite = current_user.favorites.create(micropost_id: params[:micropost_id])
nasuk47

2020/12/30 06:10

よかったです。 解決方法を記載の上この質問を解決済みにしていただけると幸いです。
guest

回答1

0

自己解決

user = Favorite.find(params[:micropost_id])
current_user.like(user)
の部分を以下のように変更したらうまくいきました。
@favorite = current_user.favorites.create(micropost_id: params[:micropost_id])

投稿2021/01/04 09:38

naoya0922

総合スコア23

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問