0

date_string というメソッドを持つモデルがあります。このメソッドのポイントは、共有ビューで使用されたときに書式設定された日付文字列を返すことです。これがビューコードです。

<div class="field control-group">
<div class="control-label">
  <%= f.label :business_hour , :date_string %>
</div>

このapi docのように機能する f.label 呼び出しを期待しています。 :business_hour がオブジェクトで、 :date_string がメソッドです。ただし、ビューに表示されるのは文字列「Date string」または「date_string」だけです。

ビュークラスがモデルのプロパティではなくメソッドを呼び出すようにするためのヘルプは大歓迎です。

営業時間コード

class BusinessHour < ActiveRecord::Base
  attr_accessible :business_hourable_id,
              :business_hourable_type,
              :close_time, :day, :open_time,
               :order , :business_date
  belongs_to :business_hourable , :polymorphic => true

def date_string()
  if business_date.nil?
   return ''
 else
    return_string = business_date.strftime( '%a %b\  %e ,  %Y' )
 end
end

終わり

完全な部分コード (shared/business_hours) は次のとおりです。

<div class="field control-group">
  <div class="control-label funkiness">
    <%= F.label :business_hour , :date_string %>
  </div>
  <div class="controls">
    <%= f.select :open_time, available_hours, :include_blank => true  %>
  </div>
  <div class="control-label">
   <%= f.label :open_time, 'Close Time' %>
  </div>
  <div class="controls">
   <%= f.select :close_time, available_hours, :include_blank => true  %>
  </div>
 </div>

_form <%= form_for (@some_show), html の関連部分は次のとおりです: {class: "form-horizo​​ntal pull-left"} do |f| %> ... <%= f.fields_for :business_hours do |営業時間| %> <%= render :partial => 'shared/business_hours', :locals => {:f => operating_time} %> <% end %>

最後に、コントローラーの編集アクションを次に示します。

# GET /some_shows/1/edit
def edit
  @some_show = SomeShow.find(params[:id])
end
4

1 に答える 1