Home>

I'm new to rails.
Problem: def create
user = Favorite.find (params [: micropost_id]):
[Couldn't find Favorite with'id'= micropost_id]
I get an error,
① How can I find the micropost_id?

(2) like, dislike, already_favorite written in the user model? Is it written correctly?
In ↓, I write a method in the user model and use it in the favorite controller.

class User</pre>
<p>Below is #favorite_controller</p>
<pre><code>class FavoritesController</pre>
<p><br />
routes ↓</p>
<pre><code>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
Partial file (like link)
<% microposts.each do | micropost |%><li>    " alt = "">                    <% = link_to micropost.user.name, user_path (micropost.user)%><span>posted at<% = micropost.created_at%></span>                      <p><% = micropost.content%></p>                      <% 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'Unlike', "/ favorites/# {: micropost_id}/create", method :: delete%>      <% else%>         <% = link_to'Like', "/ favorites/# {: micropost_id}/create", method:: post%>      <% end%>
  • Answer # 1

    user = Favorite.find (params [: micropost_id])
    current_user.like (user)
    I changed the part of to the following and it worked.
    @favorite = current_user.favorites.create (micropost_id: params [: micropost_id])

Related articles