Home>

I'm in the process of making a Twitter clone.
I would like to install a "Add/Delete Favorites" button, but I asked a question because an error occurred when I created a partial file and tried to install the button.

As a result of investigation, it seems that there is no file for favorites/_favorite_button, but I stumbled because I did not understand why this error occurred even though there was a file.

Code where the error is occurring

views/micropost/_microposts.html.erb

<ul><% microposts.each do | micropost |%><li>    " alt = "">                    <% = link_to micropost.user.name, user_path (micropost.user)%><span>posted at<% = micropost.created_at%></span>                      <% if current_user == micropost.user%>        <% = link_to "Delete", micropost, method:: delete, data: {confirm: "You sure?"},

 class:'btn btn-danger btn-sm'%>      <% end%>        <% = render'favorites/favorite_button', micropost: @micropost%>                      <p><% = micropost.content%></p>              </li><% end%><% = paginate microposts%></ul>
Source code written when adding "favorite function"

db/migrate/20201012042419_create_favorites.rb

class CreateFavorites</pre>
<p>models/favorite.rb</p>
<pre><code>class Favorite</pre>
<p>user.rb</p>
<pre><code>class User   #Favorite function
  def favorite (micropost)
    self.favorites.find_or_create_by (micropost_id: micropost.id)
  end
  def unfavorite (micropost)
    favorite = self.favorites.find_by (micropost_id: micropost.id)
    favorite.destroy if favorite
  end
  def likes? (micropost)
    self.likes.include? (micropost)
  end
end

config/routes.rb

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
      get: likes
    end
  end
  resources: microposts, only: [: create,: destroy]
  resources: relationships, only: [: create,: destroy]
  resources: favorites, only: [: create,: destroy]
end

controller/favorites_controller.rb

class FavoritesController</pre>
<p>views/favorites/_favorite_button_html.erb</p>
<pre><code><% if current_user.likes? (Micropost)%><% = form_with (model: current_user.favorites.find_by (micropost_id: micropost.id), local: true, method:: delete) do | f |%><% = hidden_field_tag: micropost_id, micropost.id%><% = f.submit'Unfavorite', class:'btn btn-danger btn-block'%><% end%><% else%><% = form_with (model: current_user.favorites.build, local: true) do | f |%><% = hidden_field_tag: micropost_id, micropost.id%><% = f.submit'Favorite', class:'btn btn-primary btn-block'%><% end%><% end%>
Please

If there is any missing source code, I will post it.