2016-10-06 27 views
0

在我看來,我有以下代碼Rails視圖 - 每個方法單獨條目由斜線

<% @merchant.working_hours.open_hours_today.each do |h| %> 
    <%= h.open_time.to_formatted_s(:hour_and_minutes) %> - 
    <%= h.close_time.to_formatted_s(:hour_and_minutes) %> 
<% end %> 

下面的代碼顯示:

Sun: 00h00 - 04h00 08h00 - 22h00 

我想達到以下(斜槓時間之間)?

Sun: 00h00 - 04h00/08h00 - 22h00 

我試過each_with_index方法沒有成功。任何想法?

回答

2

你可以做這樣的事情。但是,我可能會在我的模型或裝飾器中創建一個輔助方法,以產生開放 - 關閉時間。如果您在應用程序的多個部分中顯示它,這將派上用場。

<%= @merchant.working_hours.open_hours_today.collect { |h| "#{h.open_time.to_formatted_s(:hour_and_minutes)} - #{h.close_time.to_formatted_s(:hour_and_minutes)} }.join('/') %> 

因此,它可能看起來像

<%= @merchant.working_hours.open_hours_today.collect { |h| h.business_hours }.join('/') %> 
+0

Kobaltz,你怎麼建議做輔助方法? 任何材料來推薦我嗎? – user1301037

+0

在模型中,您可以聲明一個方法'def business_hours .... end'並將字符串插值放在那裏。 – kobaltz

+0

但是我怎麼能通過方法內插插值的h?對不起,如果它太簡單了。我很新ROR – user1301037