我正在创建一个投票应用。用户可以投票,但如果他们已经投票,他们就无法再投票。
这是检查它的方法voted_for:
user.rb
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :votes, dependent: :destroy
has_many :vote_options, through: :votes
def voted_for?(poll)
vote_options.any? {|v| v.poll == poll }
end
end
这是我的投票形式:
轮询/ _voting_form.html.erb
<%= form_tag votes_path, method: :post, remote: true, id: 'voting_form' do %>
<%= hidden_field_tag 'poll[id]', @poll.id %>
<%= render partial: 'polls/vote_option', collection: @poll.vote_options, as: :option %>
<% if current_user.voted_for?(@poll) %>
<p>You have already voted!</p>
<% else %>
<%= submit_tag 'Vote', class: 'btn btn-lg btn-primary' %>
<% end %>
<% end %>
现在我也想在索引页面上做同样的事情,它显示了民意调查列表和每个人的按钮投票。如果用户已经投票,我想显示一个按钮VIEW。
轮询/ index.html.erb
<div class="container">
<% content_for(:page_header) {"POLLS"} %>
<%= render "filter_box" %>
<% @polls.each do |poll| %>
<div class="well">
<div class="row">
<h4><%= poll.question %>
<small>(voted:
<%= poll.votes_summary %>)</small>
</h4>
</div>
<div class="row">
<div class="col-sm-2"><%= 'OPEN' %>
<p><%= poll.open_date %></p>
</div>
<div class="col-sm-2"><%= 'CLOSE' %>
<p><%= poll.close_date %></p>
</div>
<div class="col-sm-2"><%= 'DIVISION' %>
<p><%= poll.division.try(:name) %></p>
</div>
<div class="col-sm-2">
<p>
<% if current_user.voted_for?(@poll) %>
<%= link_to 'VIEW', poll_path(poll), class: 'btn btn-primary btn-lg block' %>
<% else %>
<%= link_to 'VOTE', poll_path(poll), class: 'btn btn-primary btn-lg block' %>
<% end %>
</p>
</div>
<div class="col-sm-3">
<div class="btn-group">
<%= link_to 'Edit', edit_poll_path(poll), class: 'btn btn-default' %>
<%= link_to 'Delete', poll_path(poll), method: :delete, class: 'btn btn-danger', data: {confirm: 'Are you sure?'} %>
</div>
</div>
</div>
</div>
<% end %>
polls_controller
class PollsController < ApplicationController
before_action :authenticate_user!
def index
@q = Poll.ransack(params[:q])
@polls = @q.result.order('id ASC')
respond_to do |format|
format.html
end
end
def new
@poll = Poll.new
@poll.vote_options.build
end
def create
@poll = Poll.new(poll_params)
if @poll.save
flash[:success] = 'Poll was created!'
redirect_to polls_path
else
render 'new'
end
end
def edit
@poll = Poll.find_by_id(params[:id])
end
def update
@poll = Poll.find_by_id(params[:id])
if @poll.update_attributes(poll_params)
flash[:success] = 'Poll was updated!'
redirect_to polls_path
else
render 'edit'
end
end
def destroy
@poll = Poll.find_by_id(params[:id])
if @poll.destroy
flash[:success] = 'Poll was destroyed!'
else
flash[:warning] = 'Error destroying poll...'
end
redirect_to polls_path
end
def show
@poll = Poll.includes(:vote_options).find_by_id(params[:id])
@vote = Vote.find_by_id(params[:id])
end
private
def poll_params
params.require(:poll).permit(:question, :description, :division_id, :open_date, :close_date, vote_options_attributes: [:id, :title, :_destroy])
end
end
但如果我使用它,它会忽略第一个条件并显示所有的VOTE按钮。甚至投票我已经投了票。
我如何解决这个问题?
答案 0 :(得分:1)
您没有发布控制器操作,但您可能没有在索引操作中分配@poll
。您可能会分配@polls
的内容。在这种情况下,您需要在视图中迭代@polls
并在该集合的每个成员上运行条件逻辑。
答案 1 :(得分:1)
@poll
is not a defined variable and will be null when used in if statement.
You have poll
variable. So use poll
variable in if statement in index.html.erb instead of @poll
.