[Rails] 보기에 사람 이름을 사용하여 다형성 모델 가져오기

4429 단어

모델



class Contracts::Rental < ApplicationRecord
  belongs_to :tenant, polymorphic: true # User, Company
  belongs_to :landlord, class_name: 'Company'
  belongs_to :agency, class_name: 'Company'
  belongs_to :property, polymorphic: true # Properties::Building, Unit, Room

  class << self
    def property_types
      [Properties::Building, Properties::Unit, Properties::Room]
    end
  end
end

I18n







돕는 사람



module Contracts::RentalsHelper
  # => [["Building", "Properties::Building"], ["Unit", "Properties::Unit"], ["Room", "Properties::Room"]]
  def property_type_names
    [Contracts::Rental.property_types.map(&:model_name).map(&:human), Contracts::Rental.property_types.map(&:to_s)].transpose
  end
end

보다



  <div class="field">
    <%= form.label 'Property Type' %>
    <%= form.select(:property_type, options_for_select(property_type_names, selected: contracts_rental.property_type)) %>
  </div>

좋은 웹페이지 즐겨찾기