7

I'm working a project that has recurring, weekly events. Thus, I use several DateTime fields in a nontraditional way. What I'm working on is a FormBuilder that creates a field that outputs a select for a weekday, and a select for time. I'm using a twelve-hour plugin I found online, so that works:

class ActionView::Helpers::FormBuilder
  def dow_time(dow,time,options={})
    rval = select(dow, DateTime::DAYNAMES)
    rval += time_select(time, {:minute_step => 15, :ignore_date => false, :twelve_hour => true})
  end
end

The problem I'm having is that the weekday select doesn't actually have a default selected value. This works fine on my create pages, but not on the edit pages. dow is a symbol that references the field in the calling model where the day of the week string is "Monday", "Tuesday", etc. How can I pull that value out of the calling model using dow.

self[dow]

Doesn't work since this is in a different class.

Any ideas? Something different?

4

2 回答 2

13

如果您在 FormBuilder 中,那么您可以通过简单地使用“对象”变量来访问当前对象。

前任:

在:edit.html.erb

<% form_for(@event) do |form| %>
  <%= form.custom_datetime_select(:event_starts_at) %>
<% end %>

在你的 FormBuilder

def custom_datetime_select(field, options = {})
  start_time = object.send(field)
  ...
end

object和都object_name在您调用时为您设置form_for

有关actionpack/lib/action_view/helpers/form_helper.rb更多详细信息,请参阅。

于 2009-06-01T10:34:37.603 回答
0

这应该对你有用......

class ActionView::Helpers::FormBuilder
  def dow_time(dow_model, time, options={})
    rval = select(dowmodel, :dow, DateTime::DAYNAMES)
    rval += time_select(time, {:minute_step => 15, :ignore_date => false, :twelve_hour => true})
  end
end
于 2009-06-01T04:00:28.107 回答