gpt4 book ai didi

ruby-on-rails - 在 Ruby 中卡住一个随机数

转载 作者:数据小太阳 更新时间:2023-10-29 07:26:03 26 4
gpt4 key购买 nike

我正在尝试构建一些东西,它会从 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 路线

    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

最佳答案

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

before_filter :pickanimals, :only => :index

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

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

另外

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

关于ruby-on-rails - 在 Ruby 中卡住一个随机数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13204634/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com