include multiple column value in Ruby on rails Col

2019-08-14 14:27发布

问题:

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