Hello I am trying to solve a name error in a RAILS application. I am working on an interface which consists in a map with tiles associated to a certain number of projects.
I have the following models: 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
and 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
When I try to call the resource in IRB or in a view I get the name error output. I changed specie
to field
and species
to fields
, and it works with the same configuration.
I checked and Ruby pluralises specie
correctly to species
.
How can I get specie
to work?
UPDATE : backtrace
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)
Update the relation in your Area class to:
has_many :species, :class_name => 'Specie'
Then @area.species
should work.
As @max highlighted in the comments. The problem here is that species "is the same in both singular and plural".
The two solutions are either to use species as the model name or to declare a class name of "specie" in the related models.
I tried both, both are working. In the end I opted for @max's advice.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.