我的部分位於views/votes/_voter.html.erb中,不會在瀏覽器中呈現,並且在將渲染代碼放入answers/show中時不會觸發任何錯誤。 html.erb文件。我已經梳理了所有與SO中的partials相關的問題和答案,並且找不到與這裏發生的事情完全相關的任何事情。如果有人能幫助解決這個問題,我會很感激!部分將不會呈現在欄杆中
答案/ 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 %>
的意見/票/ _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
投票.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
這是什麼部分'如果政策(Vote.new).create'因爲它似乎是問題,因爲這回'FALSE'或'nil'?將不會導致錯誤並且不會渲染部分內容。 – engineersmnky
@engineersmnky如果政策(Vote.new).create是這樣,只有登錄用戶將能夠創建和更新自己的投票。我試着刪除這條線,部分仍然沒有渲染。任何其他想法? –
你是否嘗試刪除部分,只是直接通過渲染調用它? – engineersmnky