Note that there are some explanatory texts on larger screens.

plurals
  1. PORuby on Rails, form helper with object method
    text
    copied!<p>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.</p> <pre><code>&lt;div class="field control-group"&gt; &lt;div class="control-label"&gt; &lt;%= f.label :business_hour , :date_string %&gt; &lt;/div&gt; </code></pre> <p>I am expecting the f.label call to function like in this <a href="http://apidock.com/rails/v3.2.13/ActionView/Helpers/FormHelper/label" rel="nofollow">api doc</a>, 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'. </p> <p>Any help on getting a view class to call a method, not a property, on a model is greatly appreciated.</p> <p>Business_Hour code</p> <pre><code>class BusinessHour &lt; ActiveRecord::Base attr_accessible :business_hourable_id, :business_hourable_type, :close_time, :day, :open_time, :order , :business_date belongs_to :business_hourable , :polymorphic =&gt; true def date_string() if business_date.nil? return '' else return_string = business_date.strftime( '%a %b\ %e , %Y' ) end end </code></pre> <p>end</p> <p>Here is the full partial code(shared/business_hours):</p> <pre><code>&lt;div class="field control-group"&gt; &lt;div class="control-label funkiness"&gt; &lt;%= F.label :business_hour , :date_string %&gt; &lt;/div&gt; &lt;div class="controls"&gt; &lt;%= f.select :open_time, available_hours, :include_blank =&gt; true %&gt; &lt;/div&gt; &lt;div class="control-label"&gt; &lt;%= f.label :open_time, 'Close Time' %&gt; &lt;/div&gt; &lt;div class="controls"&gt; &lt;%= f.select :close_time, available_hours, :include_blank =&gt; true %&gt; &lt;/div&gt; &lt;/div&gt; </code></pre> <p>Here is the pertinent part of the _form &lt;%= form_for (@some_show), html: {class: "form-horizontal pull-left"} do |f| %> ... &lt;%= f.fields_for :business_hours do |operating_time| %> &lt;%= render :partial => 'shared/business_hours', :locals => {:f => operating_time} %> &lt;% end %> </p> <p>And finally, here is the edit action of the controller</p> <pre><code># GET /some_shows/1/edit def edit @some_show = SomeShow.find(params[:id]) end </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload