2014-12-02 65 views
0

我在Rails中遇到了多態性問題。未定義的局部變量或方法<object> for#<Class:0x55ebe50>

我有這樣的文件:

class CreateExecutions < ActiveRecord::Migration 
    def change 
    create_table :executions do |t| 
     t.integer :player_space_id 
     t.integer :what_id 
     t.references :what, polymorphic: true 
     t.integer :qty 
     t.integer :level 
     t.datetime :when_ready 

     t.timestamps 
    end 
    end 
end 

class Execution < ActiveRecord::Base 
    belongs_to :what, :polymorphic => true 
end 

class Element < ActiveRecord::Base 
    belongs_to :game_space 
    has_many :levels 
    has_many :player_elements 
    has_many :executions, :as => what 
end 

class PlayerSpace < ActiveRecord::Base 
    belongs_to :game_space 
    belongs_to :user 
    has_many :executions, as: :what 
end 

當我運行的具有元件的控制器,我有這樣的錯誤:

NameError在PlayerSpacesController#顯示 未定義的局部變量或方法'什麼」爲#

願你幫我

回答

2

你有一個在Element類輕微錯字: 更改此:

class Element < ActiveRecord::Base 
    #... 
    has_many :executions, :as => what 
end 

要這樣:

class Element < ActiveRecord::Base 
    #... 
    has_many :executions, :as => :what 
end 

例如,您錯過了「what」的冒號,這意味着它不是符號,而是一個變量或方法。由於您沒有名爲what的變量或方法,Ruby正在拋出'未命名的變量或方法'錯誤。

相關問題