【发布时间】:2017-11-13 14:40:07
【问题描述】:
我正在创建一个投票应用程序。用户可以投票,但如果已经投票,则不能再次投票。
这是检查它的方法 voted_for:
用户.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 按钮。甚至我已经投票的民意调查。
我该如何修复它?
【问题讨论】:
-
添加索引方法的代码和其他也可以工作的代码。
-
您如何对民意调查进行分页?也许显示更多 index.html.erb 以显示您如何在页面上显示每个投票。
-
您好,感谢您的回复。我添加了完整的索引页面和 polls_controller
标签: ruby-on-rails