Tengo el siguiente modelo que permite votar por questions y por answers teniendo definido el modelo Vote
como un modelo polimórfico.
La parte de votos de preguntas me funciona correctamente pero no se como abordar la votación para las respuestas.
Las respuestas dependen de una pregunta, y la parte de votación de preguntas me funciona correctamente. ¿Cómo puedo implementar la votación de la pregunta en la vista show
?
vote.rb
# == Schema Information
#
# Table name: votes
#
# id :integer not null, primary key
# voteable_id :integer
# voteable_type :string
# created_at :datetime not null
# updated_at :datetime not null
# user_id :integer
#
class Vote < ApplicationRecord
belongs_to :voteable, polymorphic: true
belongs_to :user
def self.voted_by?(user, voteable)
voteable.votes.find_by(user_id: user.id)
end
end
routes.rb
Rails.application.routes.draw do
devise_for :users
resources :comments
resources :questions do
resources :answers, except: [:show] do
resources :comments, except: [:show], module: :answers
resources :votes, only:[:create,:update, :destroy], module: :answers
end
resources :votes, only:[:create,:update, :destroy], module: :questions
resources :comments, except: [:show], module: :questions
end
root 'questions#index'
end
Vista show.html.erb del modelo Question
donde se despliega toda la información relacionada con questions
, answers
y votes
y donde debo de hacer una implementación similar a lo que hago con los votes
de las questions
.
<div class="container">
<div class="row">
<div class="col-sm-8 col-sm-offset-1">
<h2 class="title-question"><%= @question.title %></h2>
</div>
</div>
<div class="questions">
<% if user_signed_in? %>
<div class="row">
<div class="col-sm-1 col-sm-offset-1">
<% vote = Vote.voted_by?(current_user, @question) %>
<% if vote %>
<%= link_to question_vote_path(@question, vote), method: :delete, class: "question-upvote upvote-active" do %>
<span class="upvote-arrow"></span>
<span class="upvote-count"><%= @question.votes.count %></span>
<% end %>
<% else %>
<%= link_to question_votes_path(@question), method: :post, class: "question-upvote" do %>
<span class="upvote-arrow"></span>
<span class="upvote-count"><%= @question.votes.count %></span>
<% end %>
<% end %>
</div>
<div class="col-sm-8 body-show-question">
<span> <%= markdown(@question.body) %> </span>
<span class="comments">
<%= render partial: "comments/comments", locals: {commentable: @question} %>
</span>
<%= render partial: "comments/form", locals: {commentable: [@question]} %>
</div>
</div>
<div class="row">
<div class="col-sm-8 col-sm-offset-1">
<div>
<h2>(<%= @question.answers.count %>) Respuesta(s)</h2>
</div>
<% @question.answers.each do |answer| %>
<div class="row">
<div class="col-sm-1 col-sm-offset-1">
<% vote = Vote.voted_by?(current_user, answer) %>
<% if vote %>
<%= link_to question_answer_vote_path(@question, answer), method: :delete, class: "question-upvote upvote-active" do %>
<span class="upvote-arrow"></span>
<span class="upvote-count"><%=answer.votes.count %></span>
<% end %>
<% else %>
<%= link_to question_answer_votes_path(@question, answer, vote), method: :post, class: "question-upvote" do %>
<span class="upvote-arrow"></span>
<% end %>
<% end %>
</div>
<div class="body-answer">
<span> <%= markdown(answer.body) %> </span>
<%= render partial: "comments/comments", locals: {commentable: answer} %>
</div>
<div class="form-comments-answer">
<span>
<%= render partial: "comments/form", locals: {commentable: [answer.question, answer]} %>
</span>
</div>
<% end %>
</div>
</div>
<div class="row">
<div class="col-sm-8 col-sm-offset-1">
<%= render partial: "answers/form", locals: {question: @question} %>
</div>
</div>
<% else %>
<div class="row">
<div class="col-sm-1 col-sm-offset-1">
<div class="question-upvote">
<span class="upvote-arrow"></span>
<span class="upvote-count"><%= @question.votes.count %></span>
</div>
</div>
<div class="col-sm-8 body-show-question">
<span> <%= markdown(@question.body) %> </span>
<span class="comments">
<%= render partial: "comments/comments", locals: {commentable: @question} %>
</span>
</div>
</div>
<div class="row">
<div class="col-sm-8 col-sm-offset-1">
<div>
<h2>(<%= @question.answers.count %>) Respuesta(s)</h2>
</div>
<% @question.answers.each do |answer| %>
<div class="body-answer">
<span> <%= markdown(answer.body) %> </span>
<%= render partial: "comments/comments", locals: {commentable: answer} %>
</div>
<% end %>
</div>
</div>
</div>
<% end %>
</div>
votes_controller.rb
class VotesController < ApplicationController
before_action :authenticate_user!
before_action :set_voteable, only: [:show, :edit, :update, :destroy]
def create
@voteable.votes.create(user: current_user)
redirect_to question_path(@voteable), notice: "A successfully voted for the question..."
end
def destroy
@voteable.votes.where(user: current_user).take.try(:destroy)
redirect_to question_path(@voteable), notice: "Successfully removed question vote"
end
end
comments
en tu pregunta anterior. Te recomiendo que repliques esa funcionalidad (excepto que, en lugar de registrar un comentario, registras el voto, tal como lo haces actualmente con preguntas) y, en caso que tengas algún problema publiques una pregunta más específica.