我無法工作的有趣代碼片段。我有以下型號/關係(不必要的代碼除外)HABTM雙嵌套fields_for
class Service < ActiveRecord::Base
belongs_to :service_category, :foreign_key => "cats_uid_fk"
belongs_to :service_type, :foreign_key => "types_uid_fk"
has_and_belongs_to_many :service_subtypes, :join_table => "services_to_service_subs"
belongs_to :service_request, :foreign_key => "audits_uid_fk"
accepts_nested_attributes_for :service_subtypes
end
class ServiceSubtype < ActiveRecord::Base
belongs_to :service_types, :foreign_key => "types_uid_fk"
has_and_belongs_to_many :services, :join_table => "services_to_service_subs"
end
顯示所有這些信息的形式:
<% form_for(@request, :url => { :action => :create }) do |form| %>
<table>
...other data...
<% form.fields_for :services do |fields| %>
<%= fields.hidden_field :cats_uid_fk %>
<%= fields.hidden_field :types_uid_fk %>
<% fields.fields_for :service_subtypes do |subtype| %>
<%= subtype.hidden_field :id %>
<% end %>
<% end %>
<p>
<%= form.submit "Create", :class=>"hargray" %>
</p>
<% end %>
而且控制器來處理提交:
def create
logger.debug params[:service_request].inspect
@request = ServiceRequest.new(params[:service_request])
if session[:cus_id]
@request.customer = Customer.find session[:cus_id]
end
begin
@request.save!
flash[:notice] = "Information submitted successfully. You will be contacted by a customer service representative regarding the services you selected."
redirect_to :controller => "customer", :action => "index"
rescue Exception => exc
flash[:notice] = "#{ format_validations(@request) } - #{exc.message}"
render :action => "new"
end
end
的HTML看起來很乾淨:
<input id="service_request_services_attributes_0_cats_uid_fk" name="service_request[services_attributes][0][cats_uid_fk]" type="hidden" value="1" />
<input id="service_request_services_attributes_0_types_uid_fk" name="service_request[services_attributes][0][types_uid_fk]" type="hidden" value="1" />
<input id="service_request_services_attributes_0_service_subtypes_attributes_0_id" name="service_request[services_attributes][0][service_subtypes_attributes][0][id]" type="hidden" value="2" />
<input id="service_request_services_attributes_0_service_subtypes_attributes_0_id" name="service_request[services_attributes][0][service_subtypes_attributes][0][id]" type="hidden" value="2" />
<input id="service_request_services_attributes_0_service_subtypes_attributes_1_id" name="service_request[services_attributes][0][service_subtypes_attributes][1][id]" type="hidden" value="4" />
<input id="service_request_services_attributes_0_service_subtypes_attributes_1_id" name="service_request[services_attributes][0][service_subtypes_attributes][1][id]" type="hidden" value="4" />
個提交的參數是這樣的:
{
...other data...
"services_attributes"=> {
"0"=> {
"types_uid_fk"=>"1",
"service_subtypes_attributes"=> {
"0"=>{"id"=>"1"},
"1"=>{"id"=>"2"},
"2"=>{"id"=>"3"}
},
"cats_uid_fk"=>"1"
}
}
}
我回來「未定義的方法‘service_subtype’爲#」的錯誤,而不是更新的唯一表是HABTM模型之間的連接表。任何想法如何解決這個問題或幕後發生的事情?我不確定我是否理解這個過程背後發生的「魔術」,以確保它能夠正常工作。似乎大多數人認爲HABTM不適用於嵌套屬性。似乎是這樣。解決問題?
剛剛實現了它的雙重生成隱藏字段的子類型。任何想法爲什麼上面的代碼會這樣做? – Lukas 2009-12-08 21:24:15