rails いいね機能
Q&A
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 %>
```
0