Problem: Trying to implement “like” function on my blog
PostIndex:
<% @posts.each do |post| %>
<tr>
<td><%= post.name %></td>
<td><%= link_to post.content, post %></td>
<td><%= post.created_at.strftime("%Y/%m/%d, %I:%M%p") %></td>
<td><%= post.view %></td>
<td><%= post.like %></td>
<td><%= post.hate %></td>
<td><%= link_to 'like', like_post_path(post), :remote => true %></td>
</tr>
<% end %>
PostController:
def like
@post = Post.find(params[:id])
@post.like += 1
@post.save
respond_to do |format|
format.js
end
end
app/views/posts/like.js.erb
$('#post').html("<%=j @post.like %>");
Question:
I believe I’m not correctly pointing at the post I’m looking at in like.js.erb.
In the index file, Simply doing <%= @post.view %> worked. But how do you do it in like.js.erb?
You need some identifier for post’s like count in your html when you want to update it after ajax call
PostIndex:
then in your app/views/posts/like.js.erb,