包括在导轨上在Collection_select红宝石多个列值。 此外格式化日期(include

2019-10-17 07:50发布

早些时候,我使用此代码

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

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

Answer 1:

参考这在你的模型前: - 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 %>


Answer 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' ) %>


文章来源: include multiple column value in Ruby on rails Collection_select. Also format the date