문제

작업할 수 없는 흥미로운 코드 세그먼트입니다.다음과 같은 모델/관계가 있습니다(불필요한 코드 제외)

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 :subtypes는 내가 하려는 일을 포착하기 위해 중첩된 속성의 마법에 대한 올바른 매개변수를 여전히 생성하지 않았습니다.

내가 끝내는 것은 다음과 같습니다.

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

이것은 다소 해킹적인 느낌이 들고 답변에 완전히 만족하는지 잘 모르겠지만 제출할 때 service_subtype_ids로 다시 구문 분석할 수 있는 숨겨진 필드에 쉼표로 구분된 목록을 넣습니다.

이 추가 가상 매개변수 없이 이를 수행하는 방법을 아는 사람이 있다면 알고 싶습니다.

도와 주셔서 감사합니다.

다른 팁

서비스 모델에서 복사 페이스트 오류가 아니라고 가정하면 문제의 원인이 될 수 있습니다.

 accepts_nested_attributes_for :services_subtypes

해야한다

 accepts_nested_attributes_for :service_subtypes

숨겨진 필드의 이중 생성에 대한 두 번째 사소한 문제는 Fields_FOR 섹션에 삽입 한 것입니다. fields_for에는 자동으로 ID에 숨겨진 필드가 포함되어 있습니다. 다음 블록에서 숨겨진 필드 라인을 제거하는 것이 안전합니다.

<% fields.fields_for :service_subtypes do |subtype| %>
  <%= subtype.hidden_field :id %>
<% end %> 
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top