3个或更多级嵌套表单Rails 4 - SImple表单

时间:2014-06-13 09:37:03

标签: ruby-on-rails forms nested simple-form

我需要创建一个包含多个模型的表单,至少五个。目前我正试图使用​​三种形式(人 - 疾病 - 治疗)使其工作。第二个第一级和第二级表单正常工作。在我看来,第三种形式甚至没有呈现。有人可以帮我这个吗?在此先感谢!!!。

表格

<%= 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+\-.]+@[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", "danielcastillomarfull@gmail.com"], ["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

2 个答案:

答案 0 :(得分:0)

您是否已测试添加

:treatments

accepts_nested_attributes_for

class Person

答案 1 :(得分:0)

最后我做了如下:

<强>表格

<%= 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 :surname, :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, my_disease.object.treatments.build 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 %>

人物模型

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

      validates_presence_of :name, :email
  #    validates :name, :length => {:maximum => 50, :too_long => "name is too long"}
  #    VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
  #    validates :email, format: { :with => VALID_EMAIL_REGEX , message: "is invalid" }

      accepts_nested_attributes_for :diseases


    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 Treatment < ActiveRecord::Base
  belongs_to :disease
  has_many :medicines
  # validates_presence_of :name, :start
  # validates :days, :allow_blank => true, :numericality => { :greater_than_or_equal_to => 1, :message => ": leave this field blank or enter a positive number" }
  # validates :name, :length => {:maximum => 50, :too_long => "is too long, you can use the description field"}
  # validates :description, :length => {:maximum => 5000, :too_long => "is too long"}
end