【问题标题】:include multiple column value in Ruby on rails Collection_select. Also format the date在 Ruby on rails Collection_select 中包含多列值。还要格式化日期
【发布时间】:2012-10-16 07:24:19
【问题描述】:

之前我用这个代码

<%= select "selected_payment", "id", @shifts.map {|u| [' ' +u.start_time.strftime("%I:%M %p") + '-' + u.end_time.strftime("%I:%M %p")+' ',u.id]} %>   to make a dropdown.

现在我必须对 collection_select 做同样的事情。但我不知道该怎么做。 这将类似于下面给出的:

<%= f.collection_select :shift_id, @shifts,:id, :start_time, :prompt => true %>

我什至不能格式化日期并同时使用两个值。 请帮忙,在此先感谢

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 forms


    【解决方案1】:

    参考 this 在您的模型中,例如:- Shift

    def start_end_time
     ' ' +self.start_time.strftime("%I:%M %p") + '-' + self.end_time.strftime("%I:%M %p")+' '
    end
    

    在视图中

    <%= f.collection_select :shift_id, @shifts,:id, :start_end_time, :prompt => true %>
    

    【讨论】:

      【解决方案2】:

      我发现自己有同样的问题,我解决了这个问题......

      在我的 user.rb 中

        def first_and_last_name    
          "#{self.first_name} " " #{self.last_name}"   
        end
      

      在我看来

      <%= f.collection_select(:user_id, User.all, :id, :first_and_last_name, {}, class: 'ui search dropdown' ) %>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-12-18
        • 2012-08-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多