I realise that there are several similar questions, but none of them seem to address the issue with nested resources. I'm quite new to ROR and I am trying to implement a like/unlike using ajax with the acts_as_votable gem. I kind of understand how ajax works in Rails but I seem to have problems implementing it on my own.
The structure
My app has a list of Qs which each has a list of As. Users can 'bookmark/un-bookmark' a question or 'like/unlike' an answer. 'Downvoting' an answer like in stackoverflow is not permitted - users may only 'undo' their cast votes.
Current Attempts
This is the like snippet of my show.html.erb for a Question. It's not a separate partial file. It's within the show view.:
<% @question.answers.each do |answer| %>
*omit*
<% if current_user.id!=answer.user_id %>
<div class="heart-like">
<br>
<!-- Like Button -->
<% if current_user.liked? answer %>
<%= link_to unvote_question_answer_path(@question, answer), id: “like_#{answer.id}”, remote: :true do %>
<span class="fa fa-heart fa-2x"></span>
<% end %>
<% else %>
<%= link_to like_question_answer_path(@question, answer), id: “like_#{answer.id}”, remote: :true do %>
<span class="fa fa-heart-o fa-2x"></span>
<% end %>
<% end %>
<br>
</div>
<% end %>
My routes:
resources :questions do
member do
get "bookmark"
end
resources :answers do
member do
put "like", to: "answers#upvote"
put "unvote", to: "answers#unvote"
end
resources :comments, only: [:index, :new, :create, :destroy]
end
end
My answers controller has these two methods:
def upvote
begin
@question = Question.find(params[:question_id])
@answer = Answer.find(params[:id])
@answer.liked_by current_user
respond_to do |format|
format.html { redirect_to :back }
format.js
end
rescue ActiveRecord::RecordInvalid
render :nothing => true, :status => 404
end
end
# undo prev vote
def unvote
begin
@question = Question.find(params[:question_id])
@answer = Answer.find(params[:id])
@answer.unliked_by current_user
respond_to do |format|
format.html { redirect_to :back }
format.js
end
end
end
And this is the js I made with tremendous help from this blog, which I don't know where to put:
$(“#like_<%= @answer.id %>”).removeClass(‘fa fa-heart-o’).addClass(‘fa fa-heart’);
$(“#like_<%= @answer.id %>”).attr(“href”, ‘/answers/<%= @answer.id %>/unlike’);
$(“#likes_<%= @answer.id %>”).html(“<%= j (render partial: ‘answers/likes’, locals: {answer: @answer} ) %>”);
=> BUT I think everything should be @question.answer.id, because the answers are nested within the questions -> which I am completely confused over. But if I use @question.answer.id, what should {answer: @answer} this part look like?
And the partial is (_likes.html.erb)
<div class="num-votes">
<%= answer.votes_for.size %> Likes
</div>
So I'm confused about a few things:
- Where does _likes.html.erb and like.js.erb go in my project dir (in answers? in questions?)
- How do I write my like.js.erb file when the answers are nested (made a few attempts but all failed :( )
And how can I get this to work? (currently the like doesn't do anything... and the heart isn't displayed as well.)
Thanks in advance.