3

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.

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 - site.com:3000/posts/*post.id* - with the form data being passed of like[liked_post_id]:*post.id*, when I try to submit a like on a profile - site.com:3000/users/*user.id* - which contains a feed of posts, the form data being passed is like[liked_post_id]: (blank value)

How can I pass the post's ID within a feed of posts to the liked_post_id variable in _like.html.erb?

I have noticed that the action of the like form is /likes across the board. Would this will only work when you are on the page site.com:3000/posts/*post.id*? I'm curious if I need to modify the it so that the action of the form is /posts/*post.id*/likes when you are on the page site.com:3000/users/*user.id*

From my post view:

#views/posts/_post.html.erb:
...
  <%= render 'posts/like_form' if signed_in? %>
...

Route to proper form:

#views/posts/_like_form.html.erb:
  <div id="like_form">
  <% if current_user.likes_this?(@post) %>
    <%= render "posts/unlike" %>
  <% else %>
    <%= render "posts/like" %>
  <% end %>
  </div>

Like from:

#views/posts/_like.html.erb
  <%= form_for Like.new, :remote => true do |f| %>
    <%= f.hidden_field :liked_post_id, :value => @post.id %>
    <%= f.submit "Like" %>
  <% end %>

From profile (feed of posts):

#views/users/show.html.erb
...
  <%= render @posts %>
...

Likes controller:

#controllers/likes_controller.rb    
  class LikesController < 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
...

User model:

#models/user.rb
...
  def like!(post)
    likes.create!(liked_post_id: post.id)
  end
...

@frank-blizzard has pointed out that my form markup is an issue. On a post's page the generated markup is:

<input id="like_liked_post_id" name="like[liked_post_id]" type="hidden" value="73" /> 

While on the feed page:

<input id="like_liked_post_id" name="like[liked_post_id]" type="hidden" />
2
  • The creation of likes should be done in controller. Commented Feb 11, 2013 at 1:16
  • @Code-Source, see "Edit 1:" above, the like is being created in the like controller. Commented Feb 13, 2013 at 16:32

1 Answer 1

8

You can do something like this:

<% form_for Like.new, :remote => true do |f| %>
<%= f.hidden_field :post_id, :value => @post.id %>
<%= f.submit %>
<% end %>

The current_user.likes.build(...) part should get out of your view and inside your controller. You are using a current_user.like! method so I guess you have implemented already some method in user model to accomplish this. If not build your like in the create action of LikesController where you can access params[:like].

  def create
    @post = Post.find(params[:like][:post_id])
    current_user.likes.build(@post)
    # ...
  end

EDiT

You might need to pass your @post variable correctly into your _like_form partials, like so:

#views/posts/_post.html.erb:
...
<% if signed_in? %>
  <%= render 'posts/like_form', :post => @post %>
<% end %>
...

This will give you acceess to a post variable inside the partial so you can prepopulate your forms value with its id. See this questions as well Pass a variable into a partial, rails 3? and make sure to read up on how to pass variables correctly to partials. you can debug your views using <%= debug <variablename> %>

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for pointing out some issues I had following Ruby best practice and the location of building the likes. These are topics I'm having a hard time keeping up with while learning rails. I have updated my question with an "Edit 2" to include my like! method. Unfortunately, I am still only able to submit a like on an individual post's page, while on a profile with a feed of posts, a blank value is sent in the form data for "like[liked_post_id]"
looks like you are not passing the post variable correctly to your _like_form or _like then. try to inspect the post variable with <%= debug @post %>. Also inspect in your form markup if there is really a value inside your hidden field.
The form markup is my precise issue. On a post's page: <input id="like_liked_post_id" name="like[liked_post_id]" type="hidden" value="73" /> On the feed page: <input id="like_liked_post_id" name="like[liked_post_id]" type="hidden" /> Where would I correct the post variable?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.