2011-10-19 13 views
1

私は、ビジネスディレクトリと同様のページ構造をhttp://www.redbeacon.com/s/b/から作成するためにプロトタイプ(レール2.2.2)を作成しています。ディレクトリ構造を作成する(ネストされたルート+きれいなURL、...)

ゴールには、次のパスが必要です。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 %> 

すべてのアイデア、なぜこのエラーをメッセージがポップアップしますか?または一般的により良いアプローチのための任意のアイデア?

+0

私はこれがあなたの質問に接していることを知っていますが、私は本当にあなたが他の選択肢を持っている2.2.2のような古いものを使用しないことを勧めます。これをプロトタイプ作成している場合は、新鮮なものから始めるように思えます。 3.0シリーズがまだあなたにとって新しくなっていれば、私は少なくとも2.3.11に行くことを試みます。 – Emily

+0

これは既存のシステムですが、遅かれ早かれレール3にアップグレードする必要がありますが、それは確かです。 – hebe

答えて

2

あなたはATが見えるはずですコードの一部:

class Location < ActiveRecord::Base 
    belongs_to :states 
end 

、あなたが取得しているエラーに関連していないが、それは、

class Location < ActiveRecord::Base 
    belongs_to :state 
end 

別のノートでなければなりませんが、Rubyのプログラマは通常好みますarray.eachよりfor item in array

+0

素晴らしい作品です、ありがとう! – hebe

関連する問題