모델 :캔트 얻을 개체 특성
class Car < ActiveRecord::Base
...
belongs_to :manufacturer
...
end
class Manufacturer < ActiveRecord::Base
...
has_many :cars
...
end
자동차 컨트롤러 :
def index
@title = "All cars"
@cars = Car.paginate(:page => params[:page], :per_page => 20)
end
보기 :
<%= car.manufacturer.name %>
쇼 : 정의되지 않은 메서드`이름 '무기 호에 대한 : NilClass
승인. 보기 :
<%= car.manufacturer[name] %>
쇼 : 0x460c488>
하지만 # < 번호 정의되지 않은 지역 변수 또는 메서드 '이름'! 보기 :
<%= car.manufacturer.to_yaml %>
쇼 :
이--- !ruby/object:Manufacturer
attributes:
id: 1
name: Acura
created_at: 2011-11-30 09:59:19.750976000 Z
updated_at: 2011-12-06 10:38:46.569531000 Z
그래서 내가 어떻게 Car.manufacturer의 이름을 읽을 수 있습니까?
에 관심이있을 수 : model'을하지'belongs_to : manufacturer'. 그게 전사 오류인가요? – Chowlett
고정, thx. 자동차는 모델과 제조업체가 둘 다 있습니다 : – bravedick
'car.manufacturer [name]'이 잘못되었습니다. 아마도 car.manufacturer [: name]' – Achilles