2014-12-02 64 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正在抛出'未命名的变量或方法'错误。

相关问题