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 エラー

https://gyazo.com/1713998d17a4ec6e63d8d31f67cab9f2
↑のように'favorites_path'なんていうのは記述していないのにエラー文にそれが表示されるのはどうしてなのでしょうか?

<% if current_user.already_favorite?(micropost) %>
  <%= form_with(model: current_user.relationships.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| %>
    <%= f.hidden_field :micropost_id,  micropost.id %>
    <%= f.submit 'いいね!', class: 'btn btn-primary btn-block' %>
  <% end %>
<% end %>

0

1Answer

form_withにモデルのインスタンスを渡した際は、formのurlが自動で推測されます。

今回の場合は、Favoritesモデルのインスタンスをform_withに渡しているため、urlにfavorites_pathが自動で推測されています。
そして、Railsはfavorites_pathを探しに行くのですが、おそらくroutes.rbfavoritesのルーティングを設定していないため、undefined methodエラーが起こっています。

以下に、form_withについての記事やページを載せておきますね。


ちなみに、エディタに画像をドラッグ・アンド・ドロップすると質問に画像を載せることができます。
image.gif

0

Your answer might help someone💌