views:

86

answers:

3

Hi all!

Is there a way I can turn this:

<%= f.datetime_select :truckleft, :start_year => Date.current.year, :end_year => Date.current.year, :include_blank => true %> 

into a helper, because I need to use it a lot throughout the form

I do however would like the rest to stay in the view file, like the:

<%= form_for(@trip) do |f| %>

so far nothing worked yet

Thank you so much!

ps, i'm using Rails 3..

A: 

Have you tried something like this:

def truck_left_selector(f)
  f.datetime_select :truckleft, :start_year => Date.current.year, :end_year => Date.current.year, :include_blank => true
end

Then in your view,

<%= form_for(@trip) do |f| %>
  ...
  <%= truck_left_selector(f) %>
  ...
<% end %>
neutrino
thanks! however this only works for 1 field, I need this for at least 10 fields.. what I tried next is, adding field to it, but then I get errors..
Paintrick
A: 

got it after changing neutrino's awesome answer a little bit:

in the helper:

def truck_selector(f, field)
    f.datetime_select field, :start_year => Date.current.year, :end_year => Date.current.year, :include_blank => true
end   

in the view:

<%= truck_selector(f, :truckleft) %>
<%= truck_selector(f, :truckarrive) %>
Paintrick