Help on customizing the DateTimeInput

93 views
Skip to first unread message

Victor Martins

unread,
Jan 13, 2012, 2:15:39 AM1/13/12
to SimpleForm
I have a birthday field that is a Date field, that by default renders
has three select menus, but I need to render it has three input like
this:

<div>
<label> </label>
<input>
<input>
<input>
</div>

I've managed to do this:

<label>Hello</label>
<%=profile_form.simple_fields_for :birthday do |date| %>
<%= date.input :day, :as => :string, :label =>
false, :wrapper => false, :placeholder => "DD", :input_html =>
{ :maxlength => 2 } %>
<%= date.input :month, :as => :string, :label =>
false, :wrapper => false, :placeholder => "MM" , :input_html =>
{ :maxlength => 2 } %>
<%= date.input :year, :as => :string, :label =>
false, :wrapper => false, :placeholder => "YYYY" , :input_html =>
{ :maxlength => 4 } %>
<%- end -%>

And it worls, but is very verbose to use over and over again, so I
wish I could do a custom input for it.

So far I tried this:

class CustomDateInput < SimpleForm::Inputs::Base
def input
"#{@builder.text_field("#{attribute_name}",
input_html_options)}".html_safe +
"#{@builder.text_field(attribute_name,
input_html_options)}".html_safe +
"#{@builder.text_field(attribute_name,
input_html_options)}".html_safe
end
end

but when I try to give custom attribute_names like "#{attribute_name}
_day" it blows up saying that there is no method for that.
Can anyone help me out?
Thanks.




Carlos Antonio da Silva

unread,
Jan 13, 2012, 5:28:20 AM1/13/12
to plataformate...@googlegroups.com
The problem is that your model doesn't respond to the custom attribute you want to create. The model has to have such attributes, either db columns or virtual ones that you will handle, otherwise it'll fail with no method errors.
--
At.
Carlos Antonio

Victor Martins

unread,
Jan 13, 2012, 3:22:44 PM1/13/12
to SimpleForm
Hello António,
Thanks for your fast reply!
Since my inexperience, that didn't crossed my mind.

But, I'm curious, how do you do with your DataTimeInput?

I see in the source code only reference 1i and 4i, and not 2i or 3i.
And I don't have those attributes either.
Is this already an abstraction for the rails control?

def label_target
case input_type
when :date, :datetime
"#{attribute_name}_1i"
when :time
"#{attribute_name}_4i"
end
end

Thanks so much, and congratulations for the gem :)


On Jan 13, 10:28 am, Carlos Antonio da Silva
<carlosantoniodasi...@gmail.com> wrote:
> The problem is that your model doesn't respond to the custom attribute you
> want to create. The model has to have such attributes, either db columns or
> virtual ones that you will handle, otherwise it'll fail with no method
> errors.
>
> On Fri, Jan 13, 2012 at 5:15 AM, Victor Martins
> <corr...@victormartins.com>wrote:
> >    "#...@builder.text_field("#{attribute_name}",
> > input_html_options)}".html_safe +
> >    "#...@builder.text_field(attribute_name,
> > input_html_options)}".html_safe +
> >    "#...@builder.text_field(attribute_name,

Carlos Antonio da Silva

unread,
Jan 13, 2012, 7:07:01 PM1/13/12
to plataformate...@googlegroups.com
Hey, thank you :).

This is how Rails handles date/time selects. When you create a date_select (or use an input with :date type in SimpleForm, which will use date_select under the hood), Rails generates the inputs adding _1i, _2i, and so on to the selects, so when the form is posted, it knows how to rebuild the date/time object based on the attributes. This is called multiparameter assignment.

What SimpleForm does (from the piece of code you've sent) is just an attempt to create a label that points to the first select in a date/time select. The main date/time implementation is just delegated to Rails date/datetime/time selects.
--
At.
Carlos Antonio

Victor Martins

unread,
Jan 14, 2012, 3:43:45 AM1/14/12
to SimpleForm
Thank you for your patience, now it seems clear :)
Sometimes all this layers of "magic", abstract things so much that is
hard to know how
things work :P

On Jan 14, 12:07 am, Carlos Antonio da Silva
<carlosantoniodasi...@gmail.com> wrote:
> Hey, thank you :).
>
> This is how Rails handles date/time selects. When you create a date_select
> (or use an input with :date type in SimpleForm, which will use date_select
> under the hood), Rails generates the inputs adding _1i, _2i, and so on to
> the selects, so when the form is posted, it knows how to rebuild the
> date/time object based on the attributes. This is called multiparameter
> assignment.
>
> What SimpleForm does (from the piece of code you've sent) is just an
> attempt to create a label that points to the first select in a date/time
> select. The main date/time implementation is just delegated to Rails
> date/datetime/time selects.
>
> On Fri, Jan 13, 2012 at 6:22 PM, Victor Martins
> <corr...@victormartins.com>wrote:

Carlos Antonio da Silva

unread,
Jan 16, 2012, 5:15:33 AM1/16/12
to plataformate...@googlegroups.com
No problem, glad to help :)
--
At.
Carlos Antonio

游宗諺

unread,
Mar 6, 2015, 5:32:35 AM3/6/15
to plataformate...@googlegroups.com, cor...@victormartins.com
Hi Victor Martins
Sorry for bother you. I have almost same issue with u. But I am Rails beginner. I cant understood clearly. Could u show me some example. 
Actually I am attempt to writing a bit sample here https://gist.github.com/AndyYou/5ce254c5d1f8aa3e29e2
But it doesnt work right now. I dont know how to make name attribute like generation of date_select. 

e.g  id="attributes_name_1i" and name="model[attributes_name_(1i)]"




 
Victor Martins於 2012年1月13日星期五 UTC+8下午3時15分39秒寫道:
Reply all
Reply to author
Forward
0 new messages