Question

I have a model "Products" (which is a child model of parent model "Assortments") which can be liked by the user. Right now, you can only like the Product on the Products Show page because i'm able to find the products ID, which is what i need to like it

def like
  @assortment = Assortment.find(params[:assortment_id])
  begin
    current_user.vote_for(@product = Product.find(params[:id]))
    respond_with @product, :location => assortment_product_path
  rescue ActiveRecord::RecordInvalid
    redirect_to @product
  end
end

I would also like user's to be able to like the product on the products index page. For example, if you go to Pinterest, you can like all the products on their fluid grid layout page as well as on each pictures respective page.

This is one of those things I have yet to learn how to do with Rails. How can i accomplish this??

Thanks in advance! Happy Holidays

Was it helpful?

Solution

Assuming your like action is in your ProductsController, and you have set @products equal to the products you want for your index page, and you're using erb, put something like this in your view:

<% @products.each do |product| %>
  <%= link_to "Like", product_link_path(id: product.id) %>
<% end %>

Even if you have it set up differently, it can't be too far off from this.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top