我在创建投票时遇到问题,其中包含用户决定为候选人提供的积分值(Borda Count Method)。值在number_fields中,我希望它们保存在Vote对象中。
在我的情况下,我只能从最后一个number_field获取值,我不知道如何修复它。其他值正在被覆盖,所以我想出了一个创建点数组的想法,其中包含从许多number_field_tag获取的值并将它们从视图传递到控制器。
关于调查结构的一些事情: 有一个Surv(调查),其中包含许多民意调查(问题)。 一个民意调查包含许多投票选项。 还有投票可以节省用户的投票。
来自params的输出
client.getText("your_element_locator", function(result) {
expect(result.value).to.equal("Gmail");
console.log(msg.toString()+result.value);
});
查看:
show.html.erb
{"utf8"=>"✓", "poll"=>{"id"=>"11"}, "9"=>"17", "arr"=>"1212", "pnts"=>"", "10"=>"20", "11"=>"22", "commit"=>"Zagłosuj", "controller"=>"votes", "action"=>"create"}
_voting_form.html.erb
<p id="notice"><%= notice %></p>
<h2>Twoja ankieta:</h2>
<h1>
<%= @surv.topic %>
</h1>
<h2>
<%= election_type %>
</h2>
<%= render 'voting_form' %>
<%= link_to 'Edit', edit_surv_path(@surv) %> |
控制器:
votes_controller.rb
<%= form_tag votes_path, method: :post, remote: true, id: 'voting_form' do %>
<% if @surv.votingtype == "default" %>
<%# here is unnecessary code ... %>
<% elsif @surv.votingtype == "bordy" %> <%# BORDY %>
<% @polls.each do |poll| %>
<h2><%= poll.topic %></h2>
<h2>POLL ID: <%= poll.id %></h2>
<%= hidden_field_tag 'poll[id]', poll.id %>
<%= arr = [] %>
<%= i = 0 %>
<% poll.vote_options.each do |option| %>
<div class="form-group">
<%= content_tag(:label) do %>
<div class="select">
<%= option.title %>
<%= radio_button_tag option.poll_id, option.id %>
<%# if selected then vote is created %>
<%= number_field_tag :point %>
<%#= arr << :point %>
<%= hidden_field_tag 'pnts', arr %>
<%# some tries to pass params %>
</div>
<% end %>
<%= visualize_votes_for option %>
</div>
<% end %>
<p><b>Głosów razem: <%= poll.votes_summary %></b></p>
<% end %>
<% if current_user && current_user.voted_for3?(@surv) %>
<p>Twój głos został DO ANKIETY został zapisany.</p>
<% else %>
<%= submit_tag 'Zagłosuj', class: 'btn btn-lg btn-primary' %>
<% end %>
<% end %>
<% end %>
MODEL:
vote_option.rb
class VotesController < ApplicationController
def create
@poll = Poll.find_by_id(params[:poll][:id])
@surv = Surv.find_by_id(@poll.surv_id)
puts "Surv id:"
puts @surv.id
@surv.polls.each do |p|
puts "Poll ID (poll voted)."
puts p.id
puts "Vote option ID (vote option voted)"
puts params[p.id.to_s]
@option = p.vote_options.find_by_id(params[p.id.to_s])
if p && !current_user.voted_for?(p)
#here i would like to grab params specially specified for each poll
@vote = @option.votes.create({user_id: current_user.id, points: params[:pnts]})
#@vote = @option.votes.create({user_id: current_user.id, points: params[:point]})
puts 'POINTS'
puts params.inspect
else
render js: 'alert(\'Glosowales juz w tej ankiecie!\');'
return
end
# end
end
@surv.update_attribute(:actual_votes, @surv.actual_votes+1)
if (@surv.maximum_votes > 0 && @surv.actual_votes >= @surv.maximum_votes)
respond_to do |format|
format.html { redirect_to terminate_surv_path(@surv), notice: 'Ostatni głos został oddany' }
format.js {render inline: "location.reload();" }
end
end
end
def vote_params
params.require(:vote).permit(:points)
end
end
vote.rb
class VoteOption < ActiveRecord::Base
belongs_to :surv
accepts_nested_attributes_for :surv
belongs_to :poll
accepts_nested_attributes_for :poll
has_many :votes, dependent: :destroy
has_many :users, through: :votes
validates :title, presence: true
end
如果您需要一些额外的代码或信息来帮助我解决问题,请告诉我们。提前谢谢