首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在Ruby中冻结随机数

在Ruby中冻结随机数
EN

Stack Overflow用户
提问于 2012-11-03 07:56:57
回答 1查看 269关注 0票数 4

我正在尝试构建一些东西,它将在Ruby中(使用Rails)从列表中随机选择两个对象,然后对这些对象执行基本操作。我目前的设置包括在开始时随机挑选数字-使用这些数字从数据库中获取对象,然后呈现这些对象以进行索引。但是,现在我正在尝试使用AJAX编辑这些对象,我遇到了一个问题。

似乎,当我点击一个链接时,随机数会被重新计算,并导致我的AJAX函数无法工作。

我已经在下面包含了(我相信是)所有相关的代码,但如果您还需要了解其他内容,请告诉我。我希望获得老动物的标题和(理想情况下,他们以前的评级和)他们的新评级出现在页面的底部。

谢谢,

哑光

controllers/static_pages.rb

代码语言:javascript
运行
复制
class StaticPagesController < ApplicationController
respond_to :html, :js
before_filter :pickanimals

def pickanimals
    @allAnimals = Animal.all
    @random_no = rand(@allAnimals.length)
    @animal = @allAnimals[@random_no]
    @allAnimals.delete_at(@random_no)
    @newRandom = rand(@allAnimals.length)
    @animal2 = @allAnimals[@newRandom]
end

  def index
    respond_to do |format|
        format.html
        format.js
    end
end

  def help
  end

  def about
  end

  def contact
  end

def league
end

def voting
    @votedAnimal = Animal.find(params[:id])
    if @votedAnimal == @animal
        @animal.rating += 1
        @animal2.rating -= 1
    else
        @animal.rating -= 1
        @animal2.rating += 1    
    end
    Animal.transaction do
        @animal.save!
        @animal2.save!
    end
    respond_to do |format|
        format.html { redirect_to root_path }
        format.js  
    end
end
end

app/views/static_pages/index.html.erb

代码语言:javascript
运行
复制
<div class="center hero-unit">
<h1>Animal Attack</h1>
<p> Who will win when nature collides? </p>
<div class="animalcontainerright">
<h2> <%= @animal.name %> </h2> 
<%= link_to image_tag(@animal.attachment.url(:large)), voting_path(@animal.id), :remote => true, :confirm => "Rating:  "+@animal.rating.to_s  %>
</div>
<div class="animalcontainerleft">
<h2> <%= @animal2.name %> </h2>
<%= link_to image_tag(@animal2.attachment.url(:large)), voting_path(@animal2.id), :remote => true, :confirm => "Rating:  "+@animal2.rating.to_s  %>
</div>
<div id="animalfacts"></div>

routes.rb

代码语言:javascript
运行
复制
AnimalAttack::Application.routes.draw do

resources :animals

root to: 'static_pages#index'
match '/help' => 'static_pages#help'
match '/about' => 'static_pages#about'
match '/contact' => 'static_pages#contact'
match '/league' => 'static_pages#league'
match '/voting/:id' => 'static_pages#voting'

end

rake routes

代码语言:javascript
运行
复制
    animals GET    /animals(.:format)          animals#index
        POST   /animals(.:format)          animals#create
 new_animal GET    /animals/new(.:format)      animals#new
edit_animal GET    /animals/:id/edit(.:format) animals#edit
 animal GET    /animals/:id(.:format)      animals#show
        PUT    /animals/:id(.:format)      animals#update
        DELETE /animals/:id(.:format)      animals#destroy
   root        /                           static_pages#index
   help        /help(.:format)             static_pages#help
  about        /about(.:format)            static_pages#about
contact        /contact(.:format)          static_pages#contact
 league        /league(.:format)           static_pages#league
               /animals(.:format)          animals#new
               /voting/:id(.:format)       static_pages#voting
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-11-03 08:06:55

我猜是因为你的:before_filter @animal被重新分配了。除非您另有说明,否则它将在所有操作之前执行(因此在每个控制器操作之前重新分配@animal )。您可能希望将其限制为只需要新生成的随机数的方法。例如:

代码语言:javascript
运行
复制
before_filter :pickanimals, :only => :index

还有。你知道Array#sample吗?这是一种从数组中选取随机元素的内置方法。你可能会发现它在这里很有用:

代码语言:javascript
运行
复制
@animal, @animal2 = @allAnimals.sample(2)

附加

像这样依赖控制器中的实例变量是在函数之间传递信息的一种非常脆弱的方式。如果您的控制器需要知道两个型号的id,以便根据用户选择递增或递减每个型号的计数器,为什么不使用表单将这两个id发送到此控制器操作?到目前为止,您只需要两个字段:chosen_animal_idreject_animal_id

票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13204634

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档