سؤال

شريحة مثيرة للاهتمام من التعليمات البرمجية التي لا أستطيع الوصول إليها. لدي النماذج / العلاقات التالية (رمز غير ضروري مستبعدة)

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 "for #" والجدول الوحيد الذي لم يتم تحديثه هو جدول الصلة بين نماذج Habtm. أي فكرة عن كيفية حل هذا أو ما يحدث وراء الكواليس؟ لست متأكدا من أنني أفهم "السحر" الذي يحدث وراء هذا الإجراء لرؤيته يعمل. يبدو أن معظمهم يقولون أن Habtm لا يعمل مع سمات متداخلة. يبدو أن هذا هو الحال. العمل حول؟

هل كانت مفيدة؟

المحلول 2

وجدت هذا الخطأ، كان في بلدي الارسال. على أي حال، كانت الحقول_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

هذا هو مقلوت كيندا أشعر أنني لست متأكدا من أنني راض تماما عنها كإجابة، لكنها تضع قائمة مفصولة بفواصل في حقلي المخفي الذي يمكنني تحليله إلى Service_subtype_ids مرة أخرى على إرسال.

إذا كان أي شخص يعرف كيفية القيام بذلك دون هذا التعرف الظاهري الإضافي، أحب أن أعرف.

شكرا للمساعدة.

نصائح أخرى

على افتراض أن هذه ليست خطأ نسخ لصق في طراز الخدمة، فقد يكون مصدر مشكلتك.

 accepts_nested_attributes_for :services_subtypes

يجب ان يكون

 accepts_nested_attributes_for :service_subtypes

يجب أن تكون الحجج الأولى التي يقبلها_nester_attributes_for جمعية كما هو محدد بواسطة عبارة Has_many، has_and_belongs_to_many أو ينتمي إليها.

المشكلة الثانية الثانية حول المولد المزدوج من الحقل المخفي تأتي من إدراجها في قسم الحقول_فور. يتضمن الحقول_FOR تلقائيا حقل مخفي للمعرف. مما يجعلها آمنة لإزالة خط الحقل المخفي من الكتلة التالية.

<% fields.fields_for :service_subtypes do |subtype| %>
  <%= subtype.hidden_field :id %>
<% end %> 
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top