include multiple column value in Ruby on rails Collection_select. Also format the date

醉酒当歌 提交于 2019-12-24 17:26:36

问题


Earlier i was using This code

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

Now i have to do same thing with collection_select. But i'm not able to figure how to do it. It will be something like the one given below :

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

I can not even format the date and use two values at the same time. Please help,Thanks in Advance


回答1:


Ref this In your model for ex:- Shift

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

In views

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



回答2:


i found myself with the same question and i solve it doing this...

at my user.rb

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

at my view

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


来源:https://stackoverflow.com/questions/12909492/include-multiple-column-value-in-ruby-on-rails-collection-select-also-format-th

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!