Note that there are some explanatory texts on larger screens.

plurals
  1. POFind polymorphic association for destroy action
    text
    copied!<p>So I have a polymorphic favorite model that allows users to favorite all kinds of different things (and currently works ok methinks). I'm now trying to destroy their favorites but having difficulties selecting the favorite to destroy, I don't know how to start the form_for. </p> <p>My create looks like this:</p> <p>units_controller.rb - here I create the @favorite for the form_for</p> <pre><code> @favorite = Favorite.new @favorable = @unit </code></pre> <p>_favorite.html.erb</p> <pre><code>&lt;%= form_for([@user, @favorable, @favorite]) do |f| %&gt; &lt;div&gt;&lt;%= f.hidden_field :favorer %&gt;&lt;/div&gt; &lt;%= f.submit "Favorite", class: "btn btn-large btn-primary" %&gt; &lt;% end %&gt; </code></pre> <p>favorites_controller.rb</p> <pre><code> def create @user = User.find(params[:user_id]) @favorite = @favorable.favorites.new(params[:favorite]) @favorite.favorer = current_user @favorite.save # Favorite id: 1, favorer: 2, favorable_id: 1, favorable_type: "Unit" respond_to do |format| format.html { redirect_to :back } format.js end end </code></pre> <p>Here's what I've got so far on delete but I'm unsure how to call the form_for:</p> <p>_unfavorite.html.erb</p> <pre><code>&lt;%= form_for([@user, @favorable, @favorite], # problems here as @favorite is Favorite.new html: { method: :delete }, remote: true) do |f| %&gt; &lt;%= f.submit "Unfavorite", class: "btn btn-large" %&gt; &lt;% end %&gt; </code></pre> <p>favorites_controller.rb</p> <pre><code>def destroy @favorite = Favorite.find(params[:id]) @favorite.destroy respond_to do |format| format.html { redirect_to @user } format.js end end </code></pre> <p>My follow model uses this, but I'm not sure how to nest it under /users/1/units/1/favorites</p> <pre><code>current_user.relationships.find_by_followed_id(@user) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload