Freeze random number in Ruby

I am trying to create something that will randomly select two objects from a list in Ruby (using Rails), and then perform basic operations on these objects. My current setup involves randomly selecting numbers at the beginning - using these numbers to get objects from the database, and then rendering these objects for indexing. However, now I am trying to edit these objects using AJAX. I have a problem.

It seems that when I click on the link that a random number is recounted and causes my AJAX function to not work.

I have included (what I think) all the relevant codes below, but let me know if you need to see anything else to figure it out. I am looking to get the titles of old animals and (ideally their previous grades) and their new grades appear at the bottom of the page.

Thanks,

Matt

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 
+4
source share
1 answer

I assume @animal reassigned due to your :before_filter . Unless you say otherwise, it will be executed before all actions (thus reassigning @animal before each controller action). You probably want to limit it only to methods that need a recently reconstructed random number. For instance:

 before_filter :pickanimals, :only => :index 

Besides. Do you know about Array # sample ? This is a built-in way to select a random element from an array. You may find this useful here:

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

ADDITIONALLY

Relying on instance variables in a controller like this is a pretty fragile way to pass information between functions. If your controller needs to know that the id two models has counters on each incremental or reduced basis based on the user's choice, why not just use the form to submit these two id to this controller action? For now, you only need two fields: chosen_animal_id and reject_animal_id .

+8
source

Source: https://habr.com/ru/post/1443758/


All Articles