模型
class Answer < ActiveRecord::Base
def self.energy(v, w)
a = self.where('energy_id = ? AND weight = ?', v, w)
a.offset(rand(a.count)).first.name
end
看法
<%= form_for(@answer) do |f| %>
<%= f.submit "#{Answer.energy(3, 1)}", name: "answer", class: "btn" %>
<%= f.submit "#{Answer.energy(4, 1)}", name: "answer", class: "btn" %>
<% end %>
我有那个,它正确地返回一个随机值。虽然我调用了 36 次(18 对 2),但我不希望相同的值返回不止一次。我尝试了各种.pop
变化,但每次都失败了。
谢谢您的帮助!
对于电话,我正在使用这些表单按钮,所以我选择了:
<% names = [] %>
<div id='one' class='center'>
<%= form_for(@answer) do |f| %>
<%= f.submit "#{record = Answer.energy(3, 1, names)}", name: "answer", class: "btn btn-large btn-primary" %>
<% names << record %>
<%= f.submit "#{record = Answer.energy(4, 1, names)}", name: "answer", class: "btn btn-large btn-primary" %>
<% names << record %>
<% end %>
</div>
~~ 17 more times ~~
奇迹般有效!感谢jvnill的帮助!