title: Passing an instance variable into a form - rails
body: <p>This is likely an error due to my minimal understanding of Rails and how to use variables across models, so if there is more code needed to answer it or if my terminology is incorrect, let me know and I will gladly update the question. </p>
  
  <p>I have a feed of posts that I want a user to be able to "like." While the following code allows likes to work on an individual post's page - <code>site.com:3000/posts/*post.id*</code> - with the form data being passed of <code>like[liked_post_id]:*post.id*</code>, when I try to submit a like on a profile - <code>site.com:3000/users/*user.id*</code> - which contains a feed of posts, the form data being passed is <code>like[liked_post_id]:</code> (blank value)</p>
  
  <p>How can I pass the post's ID within a feed of posts to the <code>liked_post_id</code> variable in <code>_like.html.erb</code>? </p>
  
  <p>I have noticed that the action of the like form is <code>/likes</code> across the board. Would this will only work when you are on the page <code>site.com:3000/posts/*post.id*</code>? I'm curious if I need to modify the it so that the action of the form is <code>/posts/*post.id*/likes</code> when you are on the page <code>site.com:3000/users/*user.id*</code></p>
  
  <p>From my post view:</p>
  
  <pre><code>#views/posts/_post.html.erb:
  ...
    &lt;%= render 'posts/like_form' if signed_in? %&gt;
  ...
  </code></pre>
  
  <p>Route to proper form:</p>
  
  <pre><code>#views/posts/_like_form.html.erb:
    &lt;div id="like_form"&gt;
    &lt;% if current_user.likes_this?(@post) %&gt;
      &lt;%= render "posts/unlike" %&gt;
    &lt;% else %&gt;
      &lt;%= render "posts/like" %&gt;
    &lt;% end %&gt;
    &lt;/div&gt;
  </code></pre>
  
  <p>Like from:</p>
  
  <pre><code>#views/posts/_like.html.erb
    &lt;%= form_for Like.new, :remote =&gt; true do |f| %&gt;
      &lt;%= f.hidden_field :liked_post_id, :value =&gt; @post.id %&gt;
      &lt;%= f.submit "Like" %&gt;
    &lt;% end %&gt;
  </code></pre>
  
  <p>From profile (feed of posts):</p>
  
  <pre><code>#views/users/show.html.erb
  ...
    &lt;%= render @posts %&gt;
  ...
  </code></pre>
  
  <p>Likes controller:</p>
  
  <pre><code>#controllers/likes_controller.rb    
    class LikesController &lt; ApplicationController
    before_filter :signed_in_user
  
    def create
      @post = Post.find(params[:like][:liked_post_id])
      current_user.like!(@post)
      respond_to do |format|
        format.html { redirect_to root_url }
        format.js
      end
    end
  ...
  </code></pre>
  
  <p>User model:</p>
  
  <pre><code>#models/user.rb
  ...
    def like!(post)
      likes.create!(liked_post_id: post.id)
    end
  ...
  </code></pre>
  
  <p>@frank-blizzard has pointed out that my form markup is an issue. On a post's page the generated markup is:</p>
  
  <pre><code>&lt;input id="like_liked_post_id" name="like[liked_post_id]" type="hidden" value="73" /&gt; 
  </code></pre>
  
  <p>While on the feed page: </p>
  
  <pre><code>&lt;input id="like_liked_post_id" name="like[liked_post_id]" type="hidden" /&gt;
  </code></pre>