LoginSignup
0
0

More than 1 year has passed since last update.

お気に入り機能にてlikes#destroyがうまくいかない

Posted at

お気に入り機能にてlikes#destroyがうまくいかない

結論

いいねの解除のlink_toのpathには投稿に関する情報とユーザーに関する情報を渡さなければならない

結論にたどり着くまでに実践したこと

index.html.erb
<%= link_to tweet_like_path(tweet), method: :delete, id: "like-createBtn", class: "like-btn delete-likeBtn" do %>
  <i class="fas fa-heart"></i>
<% end %>

1.rails routesにlikes#destroyのPrefixはあってるか。

2.methodが正しい情報になっているか。今回ならdeleteであっていました。

3.prefixに必要な引数は入っているか。今回ならtweet、ここが原因なのですが最初はあっていると思っていました。

間違いがわかった理由

再びrails routesで間違いがないか確認していたところlikes#destroyのURI Patternが/tweets/:tweet_id/likes/:id(.:format)となっていることに着目しました。likes#createと違い、tweet_id以外に:idが必要担っているようです。そこでtweetの情報以外にuserの情報を渡したところうまくいきました。

index.html.erb
<%= link_to tweet_like_path(tweet, tweet.user), method: :delete, id: "like-createBtn", class: "like-btn delete-likeBtn" do %>
  <i class="fas fa-heart"></i>
<% end %>

まとめ

今後ルーティング系でうまくいかないときは1.prefixの確認、2.method確認、3.URI Pattern確認、4.link_toのpathの引数が適切かに順で確認しようと思います。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0