我需要創建一個表單,其中至少有五個模型。目前我正試圖使用三種形式(人 - 疾病 - 治療)。第二個第一級和第二級表單工作正常。我認爲第三種形式甚至沒有提供。有人可以幫助我嗎?提前致謝!!!。3個或更多級別嵌套表單Rails 4 - SImple表格
表
<%= simple_form_for @person, :html => { id: "smart-form-register" , class: "smart-form client-form" } do |f| %>
<header>
Por favor, completa el formulario
</header>
<fieldset>
<h2>Datos de contacto e información general</h2><br>
<section>
<label class="input">
<%= f.input :phone, label: 'Teléfono' %>
</section>
<div class="row">
<section class="col col-2">
<%= f.input :name, :input_html => { :value => current_user.name}, :as => :hidden %>
</section>
<section class="col col-2">
<%= f.input :lastname, :input_html => { :value => current_user.lastname}, :as => :hidden %>
</section>
<section class="col col-6">
<%= f.input :email, :input_html => { :value => current_user.email}, :as => :hidden %>
</section>
</div>
</fieldset>
<%= f.simple_fields_for :diseases do |my_disease| %>
<fieldset>
<h2>¿Padeces alguna enfermedad?</h2><br>
<section>
<label class="input">
<%= my_disease.input :name, label: 'Nombre de la enfermedad' %>
</section>
<div class="row">
<section class="col col-6">
<%= my_disease.input :start, :as => :date_picker, label: 'Inicio de la enfermedad' %>
</section>
<section class="col col-6">
<%= my_disease.input :end, :as => :date_picker, label: 'Fin de la enfermedad' %>
</section>
</div>
<div class="row">
<section class="col col-6">
<%= my_disease.input :chronical, as: :boolean, :html => {type: "checkbox" }, label: false, inline_label: 'Enfermedad crónica' %>
</section>
<section class="col col-6">
<span class="">
<%= my_disease.input :unfinished, as: :boolean, :html => {type: "checkbox" }, label: false, inline_label: 'Enfermedad actual' %>
</span>
</section>
</div>
<section>
<%= my_disease.input :description, label: 'Descripción de la enfermedad' %>
</section>
</fieldset>
<fieldset>
<section>
<%= my_disease.simple_fields_for :treatments do |my_treatment| %>
<%= my_treatment.input :name, label: 'Nombre del tratamiento' %>
<% end %>
</section>
</fieldset>
<% end %>
<footer><button type="submit" class="btn btn-primary">Register</button></footer>
<% end %>
人控制器
def person_params
params.require(:person).permit(:name, :surname, :gender, :birthdate, :bloodtype, :user_id, :phone, :email, diseases_attributes: [:id, :name, :description, :start, :end, :chronical, :description, :unfinished, treatments_attributes: [:id, :name]])
end
疾病控制器
def disease_params
params.require(:disease).permit(:name, :start, :end, :chronical, :unfinished, :description, :person_id, :person_id, treatments_attributes: [:id, :name])
end
疾病模型
class Disease < ActiveRecord::Base
belongs_to :person
has_many :treatments
# validates_presence_of :name, :start
# validates :name, :length => {:maximum => 50, :too_long => "is too long, you can use the description field"}
# validate :start_must_be_before_end, :unless => [:chronical, :unfinished], :presence => true
# validates :end, :presence => true, :unless => [:chronical, :unfinished], :presence => true
# validates :description, :length => {:maximum => 5000, :too_long => "is too long"}
accepts_nested_attributes_for :treatments
def start_must_be_before_end
if self[:end] < self[:start]
errors.add(:start, "must be before end time")
return false
else
return true
end
end
end
角色模型
class Person < ActiveRecord::Base
belongs_to :user
has_many :diseases, :dependent => :destroy #if you delete a person you also delete all diseases related
has_many :appointments, :dependent => :destroy
has_many :treatments, through: :diseases
validates_presence_of :name, :email
validates :name, :length => {:maximum => 50, :too_long => "name is too long"}
VALID_EMAIL_REGEX = /\A[\w+\-.][email protected][a-z\d\-.]+\.[a-z]+\z/i
validates :email, format: { :with => VALID_EMAIL_REGEX , message: "is invalid" }
accepts_nested_attributes_for :diseases
accepts_nested_attributes_for :treatments
end
終端
User Load (0.8ms) SELECT "users".* FROM "users" WHERE "users"."id" = 60 ORDER BY "users"."id" ASC LIMIT 1
Unpermitted parameters: treatments
(0.2ms) BEGIN
SQL (0.7ms) INSERT INTO "people" ("created_at", "email", "name", "phone", "surname", "updated_at", "user_id") VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING "id" [["created_at", Fri, 13 Jun 2014 14:17:12 UTC +00:00], ["email", "[email protected]"], ["name", "Daniel"], ["phone", "123123"], ["surname", "Castillo"], ["updated_at", Fri, 13 Jun 2014 14:17:12 UTC +00:00], ["user_id", 60]]
SQL (0.6ms) INSERT INTO "diseases" ("chronical", "created_at", "description", "end", "name", "person_id", "start", "unfinished", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9) RETURNING "id" [["chronical", false], ["created_at", Fri, 13 Jun 2014 14:17:12 UTC +00:00], ["description", ""], ["end", Fri, 13 Jun 2014], ["name", "desidia"], ["person_id", 161], ["start", Thu, 12 Jun 2014], ["unfinished", false], ["updated_at", Fri, 13 Jun 2014 14:17:12 UTC +00:00]]
(51.7ms) COMMIT
可您發佈人模式嗎? – Pavan
可能是你應該添加'has_many:治療,通過:疾病'給你的人模型 – Pavan
只有這樣?我應該改變模型中的其他任何東西嗎?我已經嘗試了您的建議,但仍然無效。感謝您的幫助,Pavan – user3402629