0
votes

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
1
Can you post the entire view and relevant model code?zeantsoi
@zeantsoi please see the question for the edit and model code.Black Dynamite
I see. It would help if you could also post the full view code, as well as the controller action you're trying to invoke.zeantsoi
@zeantsoi Okay, I've added some more information.Black Dynamite
Any luck with this yet?zeantsoi

1 Answers

0
votes

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.