的代码,我不能去上班有趣的部分。我有以下型号/关系(不必要的代码除外)

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不工作嵌套属性。似乎是这样。变通?

有帮助吗?

解决方案 2

发现错误后,在我的邮件程序。 在任何情况下,fields_for:亚型仍然没有产生嵌套属性的魔法正确的参数挑上我试图做

什么我最终是:

<强> new.erb

<% form.fields_for :services do |fields| %>
    <%= fields.hidden_field :wsi_web_serv_cats_uid_fk %>
    <%= fields.hidden_field :wsi_web_serv_types_uid_fk %>
    <%= fields.hidden_field :service_subs_hash %>
<% end %>

<强> service.rb

def service_subs_hash
    self.service_subtype_ids.join(", ")
end

def service_subs_hash=(ids)
    self.service_subtype_ids = ids.split(",")
end

这是有点hackish的感觉,我不知道我。我很满意它的答案,但是却让逗号分隔的列表在我的隐藏字段,我可以再次向解析上service_subtype_ids提交。

如果有人知道如何做到这一点没有这种额外的虚拟PARAM,我很想知道。

感谢您的帮助。

其他提示

假设这未在服务模型中的复制粘贴误差,也可能是您的问题的来源。

 accepts_nested_attributes_for :services_subtypes

 accepts_nested_attributes_for :service_subtypes

第一参数accepts_nested_attributes_for应如通过的has_many,has_and_belongs_to_many或belongs_to的语句定义的关联。

有关的隐藏字段的双生成你的第二个小问题来自于你将其插入fields_for部分。 fields_for自动包含了ID的隐藏字段。使其安全地从下面的块中删除隐藏字段线。

<% fields.fields_for :service_subtypes do |subtype| %>
  <%= subtype.hidden_field :id %>
<% end %> 
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top