Question

I have a model with a method called date_string. The point of this method is to return a formatted date string when being used in a share view. Here is the view code.

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

I am expecting the f.label call to function like in this api doc, with :business_hour being the object, and :date_string being the method. However, the only thing that is rendered to the view is the string 'Date string' or 'date_string'.

Any help on getting a view class to call a method, not a property, on a model is greatly appreciated.

Business_Hour code

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

end

Here is the full partial code(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>

Here is the pertinent part of the _form <%= form_for (@some_show), html: {class: "form-horizontal pull-left"} do |f| %> ... <%= f.fields_for :business_hours do |operating_time| %> <%= render :partial => 'shared/business_hours', :locals => {:f => operating_time} %> <% end %>

And finally, here is the edit action of the controller

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

Solution

So the solution that worked for me was to take the date_string off of my model, and implement it in a helper. Then, I modified my partial view so it looks like this:

<div class='field control-group hour_dropdown_2'>
 <div class='field control-group'>
  <div class="control-label">
    <label><%= get_business_hour_string(f) %></label>
  </div>
  <h4 class='no-break-h'>From</h4>
  <%= select :open_time, available_hours, :include_blank => false %>
  <h4 class='no-break-h'>To</h4>
   <%= select :close_time, available_hours, :include_blank => false %>
 </div>
</div>

My select tags are still busted and I don't know why for my _address partial I get an f.whatever, but for the BusinessHour partial I get nothing in the way of form helpers.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top