部分不会在铁轨中渲染

时间:2015-01-15 21:49:32

标签: ruby-on-rails ruby partial

我的部分位于views / votes / _voter.html.erb中,当我将渲染代码放在answers / show.html.erb文件中时,它不会在浏览器中呈现并且不会触发任何错误。我已经梳理了与SO中的部分内容相关的所有问题和答案,并且无法找到与此处发生的事情完全相关的任何内容。如果有人能帮助解决问题,我真的很感激!

answers / show.html.erb文件:

<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文件:

<% 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模型:

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

2 个答案:

答案 0 :(得分:0)

您似乎需要登录才能查看部分内容。

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

您需要登录才能投票吗?你登录了吗?如果你不是,你就不会看到这种偏见。

另一个要检查的地方是您的投票授权政策,以确保它们设置正确。

答案 1 :(得分:0)

事实证明我需要在views / questions / show.html文件中渲染部分内容。以下是我为达到预期结果所做的工作:

<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" %>