4

Ruby のリストから (Rails を使用して) 2 つのオブジェクトをランダムに選択し、それらのオブジェクトに対して基本的な操作を実行するものを構築しようとしています。私の現在のセットアップでは、最初にランダムに番号を選択します。これらの番号を使用してデータベースからオブジェクトを取得し、それらのオブジェクトをインデックスにレンダリングします。ただし、現在、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
4

1 に答える 1

8

あなた@animalのために再割り当てされていると思います:before_filter。特に指定しない限り、すべてのアクションの前に実行されます (したがって、すべてのコントローラー アクションの@animal前に再割り当てされます)。おそらく、新しく再生成された乱数を必要とするメソッドだけに制限したいと思うでしょう。例えば:

before_filter :pickanimals, :only => :index

また。Array#sampleについて知っていますか? これは、配列からランダムな要素を選択する組み込みの方法です。ここで役に立つかもしれません:

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

さらに

このようにコントローラー内のインスタンス変数に依存することは、関数間で情報を渡す方法としては非常に脆弱です。コントローラーがid2 つのモデルの s を認識して、ユーザーの選択に基づいてインクリメントまたはデクリメントされるそれぞれのカウンターを持つ必要がある場合、フォームを使用してこれらの 2 つidの s をこのコントローラー アクションに送信してみませんか? chosen_animal_idこれまでのところ、必要なフィールドはとの 2 つだけですreject_animal_id

于 2012-11-03T00:06:55.277 に答える