LoginSignup
tn-soccerball
@tn-soccerball

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

rails Couldn't find Favorite with 'id'=1

rails 初心者です。
タイトルのエラーが表示されているのですが、以下に関係しそうなファイルを乗せるので、一緒に解決お願いいたします。

やったこと、
・ルーティングのresourcesは複数形になっている。

class FavoritesController < ApplicationController

  before_action :require_user_logged_in

  def create
     micropost = Favorite.find(params[:micropost_id])
    current_user.like(micropost)
    flash[:success] = 'いいねをしました。'
    redirect_to user
  end

↓パーシャルとしてこのファイルの下で呼び出しています。

_like_button.html.erb

<% if current_user.already_favorite?(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 'いい外す', 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 'いいね', class: 'btn btn-primary btn-block' %>
  <% end %>
<% end %>

#micropost.html.erb

<ul class="list-unstyled">
  <% 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 %>
            <%= render 'favorites/like_button', micropost: micropost %>
        </div>
      </div>
    </li>
  <% end %>
  <%= paginate microposts %>
</ul>

_like_button.html.erbの already_favorite? メソッドは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 :favorites, dependent: :destroy
    has_many :fav_microposts, through: :favorites, source: :micropost


  #いいね機能

  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

  def already_favorite?(micropost)
    self.favorites.exists?(micropost.id)
  end

end

0

No Answers yet.

Your answer might help someone💌