Note that there are some explanatory texts on larger screens.

plurals
  1. POSubmit Form and Create action for a Polymorphic Association
    primarykey
    data
    text
    <p>I received help <a href="https://stackoverflow.com/questions/8890497/setting-up-a-polymorphic-association">Setting up a polymorphic association</a></p> <p>I am now having trouble implementing the submit form and create action. A user just needs to follow and unfollow each model.</p> <p>In my Follow Controller</p> <pre><code> class FollowsController &lt; ApplicationController before_filter :find_supports def create @relation = find_supports @follow = @relation.find(params[:follow]) current_user.follows.follow!(@follow) respond_to do |format| format.html { redirect_to :back } format.js end end def destroy @follows = Follow.find(params[:id]).followable current_user.unfollow!(@follows) respond_to do |format| format.html { redirect_to :back } format.js end end private def find_supports params.each do |name, value| if name =~ /(.+)_id$/ return $1.classify.constantize.find(value) else nil end end end end </code></pre> <p>In my Follow Form, which is rendered at on my polymorphic models</p> <p><strong>EDIT</strong></p> <pre><code> &lt;%= form_for @cause.follows.build(params[:follow]) do |f| %&gt; &lt;div&gt;&lt;%= f.hidden_field :followable_id %&gt;&lt;/div&gt; &lt;div&gt;&lt;%= f.hidden_field :followable_type %&gt;&lt;/div&gt; &lt;div&gt;&lt;%= f.hidden_field :follower_id %&gt;&lt;/div&gt; &lt;div class="create-button"&gt;&lt;%= f.submit "Follow" %&gt;&lt;/div&gt; &lt;% end %&gt; &lt;!-- I now get the values for followable_id and followable_type, but it wont get grab the follower_id and I get an error in the create action --&gt; </code></pre> <p>and in my User Model, I have these 3 methods</p> <pre><code> def following?(follow) follows.find_by_followable_id(follow) end def follow!(followable) follows.create![] ##I have tried many different params here end def unfollow!(followable) follows.destroy(params[:followable_id]).destroy end </code></pre> <p>I cannot get the follower_id, followable_id and followable_type to save properly. Any suggestions? Thanks in advance, I have spent many hours with this.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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