简体   繁体   中英

Ruby on Rails Joining Two Strings in Dropdown Text

I have a ruby on rails site with a page called "meetings". These meetings currently work fine, but I want to change the way that one of the drop downs displays its data. Currently, I have this in my controller/view:

Controller

@meeting_times = MeetingTime.select("id, meeting_type, meeting_at").where("
  (meeting_at > ?)", Time.now + 1.week).order("meeting_at")

View

<%= f.select :meeting_time_id, options_from_collection_for_select
  (@meeting_times, "id", "meeting_at") %>

Currently, as you can see, the dropdown just shows the meeting time. What I want to do make the dropdown show the meeting_type + meeting_time in one string. This sounds easy, however meeting_type is an int value. So, I would need to say "If meeting_type == 0 then display 'Staff Meeting' + meeting_time", and so on. How can I accomplish this without changing the database values for meeting_type?

in MeetingTime model create method

def meeting_time_display
   "#{meeting_type_names[meeting_type]} #{meeting_at}"
end

private

def meeting_type_names
   @@m_type_names ||= ['Staff Meeting', 'Type 2', ..., 'Type n']
end

In view call

<%= f.select :meeting_time_id, options_from_collection_for_select (@meeting_times, "id", "meeting_time_display") %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM