创建一个目录结构(带有嵌套路由+漂亮的url,...)

发布于 2024-12-11 00:55:16 字数 2627 浏览 2 评论 0原文

我正在开发一个原型(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情丝乱 2024-12-18 00:55:16

您应该查看的代码部分是:

class Location < ActiveRecord::Base
  belongs_to :states
end

并且应该是

class Location < ActiveRecord::Base
  belongs_to :state
end

另一个注意事项,尽管与您收到的错误无关,但 Ruby 程序员通常更喜欢 array.each 而不是 对于数组中的项目

The part of the code you should look at is:

class Location < ActiveRecord::Base
  belongs_to :states
end

and it should be

class Location < ActiveRecord::Base
  belongs_to :state
end

Another note, although not related to the error you are getting, Ruby programmers usually prefer array.each than for item in array.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文