加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 在Ruby中冻结随机数

发布时间:2020-12-17 03:39:03 所属栏目:百科 来源:网络整理
导读:我正在尝试构建一些东西,它将从 Ruby中的列表中随机选择两个对象(使用Rails),然后对这些对象执行基本操作.我当前的设置涉及在开始时随机选择数字 – 使用这些数字从数据库中获取对象,然后将这些对象呈现为索引.但是,现在我正在尝试使用AJAX编辑这些对象我遇
我正在尝试构建一些东西,它将从 Ruby中的列表中随机选择两个对象(使用Rails),然后对这些对象执行基本操作.我当前的设置涉及在开始时随机选择数字 – 使用这些数字从数据库中获取对象,然后将这些对象呈现为索引.但是,现在我正在尝试使用AJAX编辑这些对象我遇到了问题.

看来,当我点击链接时,重新计算的随机数会导致我的AJAX功能不起作用.

我已经包含了(我相信的)以下所有相关代码,但如果您需要查看其他任何内容,请告知我们.我希望得到旧动物的标题和(理想情况下他们之前的评级和)他们的新评级出现在页面底部.

谢谢,

马特

控制器/ 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

应用程序/视图/ 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),: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

耙路线

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

解决方法

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

before_filter :pickanimals,:only => :index

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

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

另外

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读