[英]Ruby on Rails: uninitialized constant, NameError
您好,我正在嘗試解決RAILS應用程序中的名稱錯誤。 我正在使用一個界面,該界面包含一個地圖,其中包含與一定數量的項目關聯的圖塊。
我有以下模型: area.rb
class Area < ActiveRecord::Base
attr_accessible :map_file_name, :tile_file_name, :alt, ...
has_many :species
has_many :maps
end
map.rb
class Map < ActiveRecord::Base
attr_accessible :alt, :file_name, :detail, :area_id
belongs_to :area
end
和specie.rb
class Specie < ActiveRecord::Base
attr_accessible :area_id, :author_id, :name, :pwclass, :pwfamily, :txtlandscape, :txtpod, :map
has_one :author
belongs_to :area
end
當我嘗試在IRB或視圖中調用資源時,出現名稱錯誤輸出。 我將specie
更改為field
,將species
更改為fields
,並且它在相同的配置下工作。
我檢查和Ruby pluralises specie
正確地species
。
我怎樣才能得到specie
來工作?
更新:回溯
Started GET "/projects/show/1" for 127.0.0.1 at Fri Mar 23 13:02:07 +0100 2018
Connecting to database specified by database.yml
Processing by ProjectsController#show as HTML
Parameters: {"id"=>"1"}
[1m[36mArea Load (1.8ms)[0m [1mSELECT 'areas'.* FROM 'areas' WHERE 'areas'.'id' = ? LIMIT 1[0m [["id", "1"]]
[1m[35mMap Load (0.4ms)[0m SELECT 'maps'.* FROM 'maps' WHERE 'maps'.'area_id' = 1
Rendered projects/show.html.erb (28.1ms)
Completed 500 Internal Server Error in 108ms
ActionView::Template::Error (uninitialized constant Area::Species):
28: end %>
29: <% else %>
30: <% end %>
31: <% for specie in @area.species %>
32: <div class="speciesmap">
33: <div id="tile_menu">
34: <a onclick="ArticleRequest('/article/show/area-07.html', 'ATLAS-06');"><img src="images/MAP/GUADALQUIVIR_MAP-BUTTONS-TILE-specie.svg" onMouseOver="this.src='images/MAP/estuario_area_136x85px-blck.svg'" onMouseOut="this.src='images/MAP/GUADALQUIVIR_MAP-BUTTONS-TILE-specie.svg'" alt="Specie"></a>
app/views/projects/show.html.erb:31:in '_app_views_projects_show_html_erb__1608931551_2294133420'
app/controllers/projects_controller.rb:22:in `show'
Rendered /Users/xxx/.rvm/gems/ruby-1.8.7-p374@ccc/gems/actionpack-3.2.5/lib/action_dispatch/middleware/templates/rescues/_trace.erb (0.7ms)
Rendered /Users/xxx/.rvm/gems/ruby-1.8.7-p374@ccc/gems/actionpack-3.2.5/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (0.6ms)
Rendered /Users/xxx/.rvm/gems/ruby-1.8.7-p374@ccc/gems/actionpack-3.2.5/lib/action_dispatch/middleware/templates/rescues/template_error.erb within rescues/layout (40.4ms)
將Area類中的關系更新為:
has_many :species, :class_name => 'Specie'
然后@area.species
應該可以工作。
作為@max在注釋中突出顯示。 這里的問題是物種“在單數和復數上都相同”。
這兩種解決方案是使用物種作為模型名稱,或者在相關模型中聲明“物種”的類名稱。
我都嘗試過,都在工作。 最后,我選擇了@max的建議。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.