Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails select_date form name

As much as I love rails, I've always hated dealing with dates in an html form...especially when the date isn't an object's property.

The select_date helper is nice, but it always generates this:

<select name="date[year]" id="date_year">
<select name="date[month]" id="date_month">
<select name="date[day]" id="date_day">

And I can't figure out a way to change the name prefix from "date" to something else. Is there a way to do this? What do you all use for date inputs when they're not an object property?

like image 613
tybro0103 Avatar asked Oct 04 '10 00:10

tybro0103


2 Answers

You can use the :prefix option to do this.

<%= select_date(nil, :prefix => 'payday') %>

Which generates this:

<select id="payday_year" name="payday[year]">
<select id="payday_month" name="payday[month]">
<select id="payday_day" name="payday[day]">

More examples here

http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html#method-i-select_date

like image 107
tfentonz Avatar answered Sep 28 '22 09:09

tfentonz


if it's not an object property, are you just using the rails date helpers? You can probably do better.

I'd just have a simple textfield, and use jquery to decorate it as a date .. check out this jQuery date drop-down.

You'd then parse it in the controller as a Date and do whatever you need to. it'll just be params[:super_cool_date] (or whatever)

like image 22
Jesse Wolgamott Avatar answered Sep 28 '22 09:09

Jesse Wolgamott