세 가지 모델, 사용자, 보고서 및 영수증이 있습니다. 사용자에게는 많은 보고서가 있고 보고서에는 많은 영수증이 있습니다.중첩 된 리소스 양식의 초기화되지 않은 상수
이제 양식을 만들거나 보고서를 편집하도록 설정했습니다. 영수증을 만들고 편집하려면 다른 양식을 중첩해야합니다. 레일 가이드 (다중 모델 양식 작성하기)를 따라 제 모델을 편집하고 빌드 라인을 양식보기에 추가했지만 '초기화되지 않은 상수'오류가 발생했습니다.
class Report < ActiveRecord::Base
belongs_to :user
has_many :receipts
attr_accessible :cash_advance, :company, :description, :end_date, :mileage, :report_name,
:start_date, :receipts_attributes
validates_presence_of :company, :description, :end_date, :report_name#, :start_date
validates_uniqueness_of :report_name
accepts_nested_attributes_for :receipts, :allow_destroy => :true,
:reject_if => proc { |attrs| attrs.all? { |k, v| v.blank? } }
end
class Receipts < ActiveRecord::Base
belongs_to :report
attr_accessible :account_code, :amount, :company_card, :date, :description, :lobbying_expense, :vendor
end
및 내 양식 : 여기
내 모델입니다 내가 편집해야하는 경우 임 확실하지<%# @report.receipts.build %>
<%= form_for([current_user,@report]) do |f| %>
...
<%= f.fields_for ([@report, @report.receipts.build ]) do |receipt| %>
...
<% end %>
<% end %>
내 경로 (하지만 난을 추가하기 전에 저도 같은 오류가 발생했습니다 영수증 리소스)
resources :users do
resources :reports do
resources :receipts
end
end
레일 가이드가 그걸 언급하지 않았기 때문에 리포트 컨트롤러를 편집하지 않았습니다. 유일한 : 끝
무엇 오전 :
데프 새로운 @report = current_user.reports.new 끝
데프 @report = current_user.reports.find ([ID] PARAMS)을 편집 내가 잘못하고있어?
편집 - form_for가 [@report, @ report.receipts.build]에 걸리는 그래서 영수증 내 양식을 변경하지만 지금은 오류 얻을 :
uninitialized constant Report::Receipt
가 어떻게이 양식을 구할 수 있습니까를 작업?
': receipt_attributes'를': receipts_attributes'로''report' 모델의'attr_accessible'에서 변경하십시오. – VenkatK
레코드를 저장하는 것만 생각합니다. 예외를 변경하면 예외가 발생하지 않기 때문입니다. – rugbert