繁体   English   中英

3个或更多级别的嵌套表单Rails 4-简单表单

[英]3 or more level nested forms Rails 4 - 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

你测试添加

:treatments

accepts_nested_attributes_for

class Person

最终我做到了:

形成

<%= 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM