비즈니스 디렉토리와 비슷한 페이지 구조를 만들려면 프로토 타입 (레일 2.2.2)을 사용하고 있습니다. http://www.redbeacon.com/s/b/.디렉토리 구조 만들기 (중첩 된 경로 + pretty urls, ...)
목표는 다음 경로가 있어야합니다. 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 %>
어떤 아이디어 왜이 오류를 메시지가 나타 납니까? 또는 일반적으로 더 나은 접근을위한 아이디어?
나는 이것이 당신의 질문에 접선 적이라는 것을 알고있다. 그러나 2.2.2의 레일즈를 사용하는 것에 대해서는 다른 선택의 여지가 없다고 충고한다. 이 프로토 타입을 작성하는 경우 새로 시작하는 것처럼 들립니다. 3.0 시리즈가 여전히 새롭다면 2.3.11로 갈 것입니다. – Emily
그것은 기존 시스템입니다 ...하지만 조만간 레일 3으로 업그레이드해야합니다. – hebe