【问题标题】:Freeze a random number in Ruby在 Ruby 中冻结一个随机数
【发布时间】:2012-10-23 16:00:41
【问题描述】:

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

似乎,当我单击一个链接时,随机数正在重新计算并导致我的 AJAX 功能不起作用。

我已在下面包含(我相信是)所有相关代码,但如果您需要查看其他内容以了解详情,请告诉我。我希望将旧动物的标题和(最好是它们以前的评级和)它们的新评级显示在页面底部。

谢谢,

马特

controllers/static_pages.rb

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

<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

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

    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

【问题讨论】:

    标签: ruby-on-rails ruby ajax ruby-on-rails-3 random


    【解决方案1】:

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

    before_filter :pickanimals, :only => :index
    

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

    @animal, @animal2 = @allAnimals.sample(2)
    

    额外

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

    【讨论】:

    • 嘿-谢谢。更改之前的过滤器似乎不起作用,认为这可能是因为我实际上是在使用索引视图来呈现我的 AJAX,所以我需要索引视图同时获取一个新的随机数而不是(我知道,糟糕的设计)。我会检查示例方法,看起来是一个更好的方法!谢谢,马特
    • OK...更多关于它现在如何不起作用的信息。当我更改before_filter 时,我在尝试访问主页时收到以下错误消息:NoMethodError in Static_pages#index,其中提供了更多信息:undefined method voting_path'` 其中voting_path 指的是索引视图中的链接。任何想法?谢谢,马特
    • Array#sample 正是我挑选两个随机数的方式。
    • @Kali_89:你确定错误是在更改过滤器后开始的吗?不知道这怎么会导致这种情况。您应该粘贴完整的输出。
    • @Kali_89:这是一个不相关的路由问题。阅读this section of rails routing guide。一旦这变得有意义,请将您的路线更改为 match '/voting/:id' =&gt; 'static_pages#voting', :as =&gt; 'voting' 之类的内容,看看是否有帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-01
    • 1970-01-01
    相关资源
    最近更新 更多