Use select_date for my model?


#1

Hi all

I have a model “party” that has a time field “starts_at”. I have created
a form to add new instances of this model.

<%= select_date Date.today, :prefix => ‘party’ %>

Sadly I get the error:

NoMethodError in Parties#add
undefined method `month=’ for #Party:0x256a600

How can I fix this problem? I remember that I saw in scaffolds that date
selectors have something like party[year(1n)] or so as their names…
How can I achieve this using the select_date helper?

Thanks a lot.
Greetings, Joshua


#2

Joshua M. wrote:

I have a model “party” that has a time field “starts_at”. I have created
a form to add new instances of this model.

<%= select_date Date.today, :prefix => ‘party’ %>

Sadly I get the error:

NoMethodError in Parties#add
undefined method `month=’ for #Party:0x256a600

Use date_select instead:

View:

<%= date_select :party, :starts_at %>

Controller:

@party = Party.new( :starts_at => Date.today )


We develop, watch us RoR, in numbers too big to ignore.


#3

Use date_select instead:

View:

<%= date_select :party, :starts_at %>

Controller:

@party = Party.new( :starts_at => Date.today )

Thanks a lot! But isn’t there also a time_select? I tried it with

<%= datetime_select :party, :starts_at, :discard_day => true,
:discard_month => true, :discard_year => true %>

but sadly the year is not hidden… :-/


#4

If you’re only storing a time of day, why don’t you have
start_hour and start_minute DB fields, and use select_hour
and select_minute helpers?

I only want to split the datetime selection for layouting purposes…
first the user can select the date, and then he can select the time. But
maybe I’ll have to put it in the same place under these conditions.

Thanks anyway. :slight_smile:


#5

Joshua M. wrote:

I only want to split the datetime selection for layouting purposes…
first the user can select the date, and then he can select the time. But
maybe I’ll have to put it in the same place under these conditions.

If you want to split the date and time you’ll have to use

View:

On: <%= select_date @party.starts_at, :prefix => ‘party[starts_at]’ %>
At: <%= select_hour @party.starts_at, :field_name =>
‘party[starts_at][hour]’ %>
<%= select_minute @party.starts_at, :field_name =>
‘party[starts_at][minute]’ %>

Controller:

@party = Party.new( params[:party] )

Party model:

def starts_at= (d)
self[:starts_at] =
Time.local(d[:year],d[:month],d[:day],d[:hour],d[:minute])
end


We develop, watch us RoR, in numbers too big to ignore.


#6

Joshua M. wrote:

Thanks a lot! But isn’t there also a time_select? I tried it with

<%= datetime_select :party, :starts_at, :discard_day => true,
:discard_month => true, :discard_year => true %>

but sadly the year is not hidden… :-/

Yes, datetime_select does not currently use the discard_year
option.

If the user is only able to select the time of day, how
is the date of the party determined? Is it always today?

If you’re only storing a time of day, why don’t you have
start_hour and start_minute DB fields, and use select_hour
and select_minute helpers?


We develop, watch us RoR, in numbers too big to ignore.