Forum: Ruby on Rails Use select_date for my model?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Fc582698581884352e745d1d4c64699d?d=identicon&s=25 Joshua Muheim (josh)
on 2006-03-21 20:07
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
C64e63b70be7dfed8b0742540b8b27e5?d=identicon&s=25 Mark Reginald James (Guest)
on 2006-03-21 21:56
(Received via mailing list)
Joshua Muheim 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.
Fc582698581884352e745d1d4c64699d?d=identicon&s=25 Joshua Muheim (josh)
on 2006-03-21 22:56
> 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... :-/
C64e63b70be7dfed8b0742540b8b27e5?d=identicon&s=25 Mark Reginald James (Guest)
on 2006-03-22 03:34
(Received via mailing list)
Joshua Muheim 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.
Fc582698581884352e745d1d4c64699d?d=identicon&s=25 Joshua Muheim (josh)
on 2006-03-22 07:08
> 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. :-)
C64e63b70be7dfed8b0742540b8b27e5?d=identicon&s=25 Mark Reginald James (Guest)
on 2006-03-22 09:17
(Received via mailing list)
Joshua Muheim 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.
This topic is locked and can not be replied to.