创建一个目录结构(带有嵌套路由+漂亮的url,...)
我正在开发一个原型(rails 2.2.2)来创建一个类似的页面结构作为 http 的业务目录://www.redbeacon.com/s/b/。
目标应该是具有以下路径:mysite.com/d/state/location/ ...显示某些内容的索引。到目前为止,我做了以下操作...
控制器和模型:
$ ruby script/generate controller Directories index show
$ ruby script/generate controller States index show
$ ruby script/generate controller Locations index show
$ ruby script/generate model State name:string abbreviation:string
$ ruby script/generate model Location name:string code:string state_id:integer
$ rake db:migrate
路线:
map.states '/d', :controller => 'states', :action => 'index'
map.locations '/d/:state', :controller => 'locations', :action => 'index'
map.directories '/d/:state/:location', :controller => 'directories', :action => 'index'
...在模型中构建了关系:
class State < ActiveRecord::Base
has_many :locations
end
class Location < ActiveRecord::Base
belongs_to :states
end
...向控制器添加了操作:
class StatesController < ApplicationController
def index
@all_states = State.find(:all)
end
end
class LocationsController < ApplicationController
def index
@all_locations = Location.find(:all)
@location = Location.find_by_id(params[:id])
end
end
class DirectoriesController < ApplicationController
def index
@location = Location.find_by_id(params[:id])
@all_tradesmen = User.find(:all)
end
end
状态索引视图
<h1>States#index</h1>
<p>Find me in app/views/states/index.html.erb</p>
<br><br>
<% for state in @all_states %>
<%= link_to state.name, locations_path(state.abbreviation.downcase) %>
<% end %>
位置索引视图
<h1>Locations#index</h1>
<p>Find me in app/views/locations/index.html.erb</p>
<br><br>
<% for location in @all_locations %>
<%= link_to location.name, directories_path(location.state.abbreviation, location.name) %>
<% end %>
但是我被困住了,我明白了以下错误消息:
NoMethodError in Locations#index
Showing app/views/locations/index.html.erb where line #6 raised:
undefined method `state' for #<Location:0x104725920>
Extracted source (around line #6):
3: <br><br>
4:
5: <% for location in @all_locations %>
6: <%= link_to location.name, directories_path(location.state.abbreviation, location.name) %>
7: <% end %>
有什么想法为什么会弹出此错误消息吗?或者通常有更好方法的想法吗?
I am working on a prototyp (rails 2.2.2) to create a similiar page structure as the business directory from http://www.redbeacon.com/s/b/.
Goal should be to have the following paths: mysite.com/d/state/location/ ...which displays an index of something. So far, I did the following...
Controllers and models:
$ ruby script/generate controller Directories index show
$ ruby script/generate controller States index show
$ ruby script/generate controller Locations index show
$ ruby script/generate model State name:string abbreviation:string
$ ruby script/generate model Location name:string code:string state_id:integer
$ rake db:migrate
routes:
map.states '/d', :controller => 'states', :action => 'index'
map.locations '/d/:state', :controller => 'locations', :action => 'index'
map.directories '/d/:state/:location', :controller => 'directories', :action => 'index'
...built in the models the relations:
class State < ActiveRecord::Base
has_many :locations
end
class Location < ActiveRecord::Base
belongs_to :states
end
...added actions to the controllers:
class StatesController < ApplicationController
def index
@all_states = State.find(:all)
end
end
class LocationsController < ApplicationController
def index
@all_locations = Location.find(:all)
@location = Location.find_by_id(params[:id])
end
end
class DirectoriesController < ApplicationController
def index
@location = Location.find_by_id(params[:id])
@all_tradesmen = User.find(:all)
end
end
The States Index View
<h1>States#index</h1>
<p>Find me in app/views/states/index.html.erb</p>
<br><br>
<% for state in @all_states %>
<%= link_to state.name, locations_path(state.abbreviation.downcase) %>
<% end %>
The Locations Index View
<h1>Locations#index</h1>
<p>Find me in app/views/locations/index.html.erb</p>
<br><br>
<% for location in @all_locations %>
<%= link_to location.name, directories_path(location.state.abbreviation, location.name) %>
<% end %>
But I am stuck, I get the following error message:
NoMethodError in Locations#index
Showing app/views/locations/index.html.erb where line #6 raised:
undefined method `state' for #<Location:0x104725920>
Extracted source (around line #6):
3: <br><br>
4:
5: <% for location in @all_locations %>
6: <%= link_to location.name, directories_path(location.state.abbreviation, location.name) %>
7: <% end %>
Any Ideas why this error message pops up? Or generally any ideas for a better approach?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该查看的代码部分是:
并且应该是
另一个注意事项,尽管与您收到的错误无关,但 Ruby 程序员通常更喜欢
array.each
而不是对于数组中的项目
。The part of the code you should look at is:
and it should be
Another note, although not related to the error you are getting, Ruby programmers usually prefer
array.each
thanfor item in array
.