0

My partial, which is located in views/votes/_voter.html.erb, is not rendering in the browser and not triggering any errors when I place the rendering code in the answers/show.html.erb file. I've already combed through all of the questions and answers related to partials in rails on SO, and can't find anything exactly related to what is happening here. If anyone can help shed any light on the problem, I'd really appreciate it!

answers/show.html.erb file:

<p id="notice"><%= notice %></p>

 <%= render partial: 'votes/voter', locals: { answer: @answer } %>

<p>
  <strong>Body:</strong>
  <%= @answer.body %>
</p>

<%= link_to 'Edit', edit_answer_path(@answer) %> |
<%= link_to 'Back', answers_path %>

views/votes/_voter.html.erb file:

<% if policy( Vote.new ).create? %>

  <div class="vote-arrows pull-left">
    <div>
      <%= link_to " ", 
        post_up_vote_path(answer), 
        class: "glyphicon glyphicon-chevron-up #{(current_user.voted(answer) && current_user.voted(answer).up_vote?) ? 'voted' : '' }", method: :post %>
    </div>

    <div>
      <strong><%= answer.points %></strong>
    </div>

    <div>
      <%= link_to " ", 
        post_down_vote_path(answer), 
        class: "glyphicon glyphicon-chevron-down #{(current_user.voted(answer) && current_user.voted(answer).down_vote?) ? 'voted' : '' }" , method: :post%>
    </div>
  </div>
<% end %>

votes_controller.rb:

class VotesController < ApplicationController
  before_action :load_answer_and_vote

  def up_vote

    if @vote
      @vote.update_attribute(:value, 1)
    else
      @vote = current_user.votes.create(value: 1, answer: @answer)
    end

    redirect_to :back
  end

   def down_vote

    if @vote
      @vote.update_attribute(:value, -1)
    else
      @vote = current_user.votes.create(value: -1, answer: @answer)
    end

    redirect_to :back
  end

  private

  def load_answer_and_vote
    @question = Question.find(params[:question_id])
    @vote = @question.votes.where(user_id: current_user.id).first
  end

 def update_vote(new_value)
    if @vote
       authorize @vote, :update?
      @vote.update_attribute(:value, new_value)
    else
       @vote = current_user.votes.build(value: new_value, answer: @answer)
       authorize @vote, :create?
       @vote.save
    end
  end
end

vote.rb model:

class Vote < ActiveRecord::Base
  belongs_to :user
  belongs_to :answer, dependent: :destroy 

  default_scope { order('created_at DESC') }

  validates :user, presence: true
  validates :answer, presence: true
  validates :value, inclusion: { in: [-1, 1], message: "%{value} is not a valid vote." }

  after_save :update_answer

  def up_vote?
    value == 1
  end

  def down_vote?
    value == -1
  end

  private

  def update_post
    answer.update_rank
  end
end

routes.rb

Languagecheck::Application.routes.draw do
  devise_for :users
  resources :users

  resources :languages do
    resources :questions, except: [:index] do 
      resources :answers
      post '/up-vote' => 'votes#up_vote', as: :up_vote
     post '/down-vote' => 'votes#down_vote', as: :down_vote
    end
   end

  resources :questions, only: [:index, :new, :create]

  get 'about' => 'welcome#about'
  root to: 'welcome#index' 
  end
Valerie Mettler
  • 475
  • 4
  • 16
  • 2
    what is this portion `if policy( Vote.new ).create?` since it seems to be the issue as this returning `false` or `nil` will cause no errors and no rendered partial. – engineersmnky Jan 15 '15 at 21:54
  • @engineersmnky the if policy( Vote.new).create is so that only users who are signed in will be able to create and update their own votes.I tried removing this line, and the partial still did not render. Any other ideas? – Valerie Mettler Jan 15 '15 at 22:17
  • Have you tried removing partial and just calling it directly through render? – engineersmnky Jan 15 '15 at 23:04
  • @engineersmnky I just tried using <%= render "votes/voter" %>, and still no luck! Is that what you were referring to? – Valerie Mettler Jan 15 '15 at 23:55
  • Unfortunately I cannot diagnose why it won't render. Everything seems to be as it should. Maybe someone else can find something that I have overlooked. Although I don't see @answer defined in the controller. – engineersmnky Jan 16 '15 at 00:18
  • Your link generation looks weird and could be the culprit. Try putting some test text in between the empty quotes. Also make sure your cache is reflecting the changes you're making. Check your console to see if the partial is being rendered. The partial is probably getting rendered, but there just isn't anything for it to output. This might help make those `link_to` calls cleaner: http://stackoverflow.com/questions/2115214/link-to-image-tag-how-to-add-class-to-a-tag. – Steve Jan 16 '15 at 00:45

2 Answers2

0

It looks like you need to be signed in to view the partial.

<% if policy( Vote.new ).create? %>

Do you need to be signed in to vote? Are you signed in? If you are not you wouldn't see this partial.

The other place to check is your vote authorization policies to ensure they are set up correctly.

Llama Moth
  • 26
  • 6
0

It turned out that I needed to render the partial in the views/questions/show.html file. Here is what I did to get the desired outcome:

<h3>Question</h3>

<div class="row">
  <div class="col-md-8">

    <p><%= @question.body %></p>
       <small>
       <%= image_tag(@question.user.avatar.tiny.url) if @question.user.avatar? %>
       submitted <%= time_ago_in_words(@question.created_at) %> ago by
       <%= @question.user.name %>
     </small>
  </div>
  <div class="col-md-4">
    <% if policy(@question).edit? %>
      <%= link_to "Edit Question", edit_language_question_path(@language, @question), class: 'btn btn-success' %>
    <% end %>
  </div>
</div>
<br/>
<h3>Answers</h3>
    <% @question.answers.each do |answer| %>

  <%= render partial: 'votes/voter', locals: { answer: @answer } %>

<div>
     <h3> 
      <%= pluralize(@answer.points, 'point') %>
     </h3>
     <small>
       <%= pluralize(@answer.up_votes, 'up vote') %>

       <%= pluralize(@answer.down_votes, 'down vote') %>
   </small>
   </div> 


      <%= simple_format answer.body %>
        <small>
       <%= image_tag(answer.user.avatar.tiny.url) if answer.user.avatar? %>
       submitted <%= time_ago_in_words(answer.created_at) %> ago by
       <%= answer.user.name %>
     </small>
    <% end %>
    <br/>
<h4>Add an Answer</h4>
<%= render "answers/form" %>
Valerie Mettler
  • 475
  • 4
  • 16