How to get multiple fields for options_from_collection_for_select

后端 未结 3 1731
你的背包
你的背包 2021-01-17 11:40

I have the following in a select_tag. It works fine. (I\'m using select_tag because it is for a search not tied to a model.)

options_from_collection_for_sele         


        
相关标签:
3条回答
  • 2021-01-17 12:01

    You can define on your model:

    def name; "#{first_name} #{last_name}";end

    and use:

    options_from_collection_for_select(@customers, :id, :name)

    0 讨论(0)
  • 2021-01-17 12:02

    This can also be done in this way, you don't need to write a method in your model.

    options_from_collections_for_select(
      @customers, :id, ->(ob) { "#{ob.first_name} #{ob.last_name}" }
    )
    
    0 讨论(0)
  • 2021-01-17 12:05

    add method full_name in your model :

    def full_name
       "#{first_name} #{last_name}"
    end
    

    and use this :

    options_from_collection_for_select(@customers, :id, :full_name)
    

    Hope this will help.

    0 讨论(0)
提交回复
热议问题