Is there a way to call a method on the data loaded into a form input in Rails? (Specifically a text_field) -


i'm using rails 3.0.5, , reasons specified in this question, need call utc_to_local method on text_field input datetime.

i've tried calling method in various ways directly in view (as starting point), can't seem find magic mix (haven't called method on form field before).

/events/edit.html.erb (full file):

<h1>edit event</h1> <%= form_for(@event) |f| %>     <%= render 'fields', :f => f %>     <div class="actions">         <%= f.submit "update event" %>     </div> <% end %> 

events/_fields.html.erb (relevant lines only:)

<div class="field">     <%= f.label      :time_start, "start time" %><br />     <%= f.text_field  :time_start, :class => "datetimefield" %> </div> 

to answer straight:

<%= f.text_field  :time_start, :value => f.object.time_start.try(:method_name) :class => "datetimefield" %> 

but... why don't use in_time_zone?

time.now.in_time_zone("eastern time (us & canada)") 

which lead creation of application helper:

def time_zoned(string)   return nil if string.nil?   begin      time = time.parse(string)   rescue     return nil   end   time.in_time_zone("eastern time (us & canada)") end 

and in view:

<%= f.text_field  :time_start, :value => time_zoned(f.object.time_start) %> 

Comments

Popular posts from this blog

c++ - Is it possible to compile a VST on linux? -

java - Output of Eclipse is rubbish -

jquery - Confused with JSON data and normal data in Django ajax request -