【问题标题】:how to render list of children class in ruby on rails?如何在 ruby​​ on rails 中呈现儿童班级列表?
【发布时间】:2013-06-20 20:41:04
【问题描述】:

你好

我通过一个小项目开始使用 Rails。所有主要的事情都在一些医生、病人和咨询之间。 我正在学习一本书来开始我的应用程序,现在它运行良好,但我仍然需要帮助来解决一些小问题!

例如,一旦创建了医生,我可以创建咨询,但我的咨询需要患者,而我不明白如何在创建咨询时呈现患者列表。

有人知道吗?

PS:这是我的代码

=> 医生

require 'digest'
class Doctor < ActiveRecord::Base
attr_accessible :birthdate, :birthplace, :city, :country, :firstname, :id_card_no, :lastname, :mail, :password, :secu_no, :street, :street_number, :zip
attr_accessor :password
validates :birthdate, :birthplace, :city, :country, :firstname, :lastname, :id_card_no, :secu_no, :street, :street_number, :zip, :presence=>true

validates :id_card_no,:secu_no, :uniqueness=>true

validates :street_number, :zip, :numericality=>true

validates :password, :confirmation => true,
            :length => { :within => 4..20 },
            :presence => true,
            :if => :password_required?

validates :mail, :uniqueness => true,
                :length => { :within => 5..50 },
                :format => { :with => /^[^@][\w.-]+@[\w.-]+[.][a-z]{2,4}$/i }

has_and_belongs_to_many :offices
has_and_belongs_to_many :specialities
has_and_belongs_to_many :secretaries
has_many :consultations

default_scope order('doctors.lastname')

before_save :encrypt_new_password

    def self.authenticate(email, password)
        user = find_by_email(email)
        return user if user && user.authenticated?(password)
    end
    def authenticated?(password) 
        self.hashed_password == encrypt(password)
    end

protected
    def encrypt_new_password
        return if password.blank?
        self.hashed_password = encrypt(password) 
    end
    def password_required?
        hashed_password.blank? || password.present?
    end
    def encrypt(string) 
        Digest::SHA1.hexdigest(string)
    end

end

=> 患者

class Patient < ActiveRecord::Base
attr_accessible :birthdate, :birthplace, :city, :country, :firstname, :id_card_no, :job, :lastname, :secu_no, :street, :street_number, :zip

validates :birthdate, :birthplace, :city, :country, :firstname, :lastname, :id_card_no, :secu_no, :street, :street_number, :zip, :presence=>true

validates :id_card_no,:secu_no, :uniqueness=>true

validates :street_number, :zip, :numericality=>true

has_many :consultations

default_scope order('patients.lastname')


end

=> 咨询

class Consultation < ActiveRecord::Base
 attr_accessible :date, :hour

validates :date, :hour, :presence=>true

belongs_to :patient
belongs_to :doctor
has_one :patient_description
has_one :consultation_file
has_and_belongs_to_many :illnesses
has_and_belongs_to_many :symptoms

end

谢谢!

托马斯

【问题讨论】:

    标签: ruby-on-rails ruby web rubygems


    【解决方案1】:

    我想你想在咨询的“患者_id”列中查看“collection_select”。

    【讨论】:

    【解决方案2】:

    我真的很喜欢Formtastic,因为它“理解”你的领域,例如自动为日期创建关联选择框或日期选择器:

    <%= semantic_form_for @consultation do |f| %>
      <%= f.inputs do %>
        <%= f.input :date %>
        <%= f.input :hour %>
        <%= f.input :doctor %>
        <%= f.input :patient %>
      <% end %>
      <%= f.actions do %>
        <%= f.action :submit, :as => :button %>
        <%= f.action :cancel, :as => :link %>
      <% end %>
    <% end %>
    

    但是,这不是一个纯粹的 Rails 解决方案,需要额外的 Gem。我不确定这是否适合您的培训目的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-12-16
      • 1970-01-01
      • 1970-01-01
      • 2013-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多