Three text fields for one database entry for rails - ruby-on-rails

I currently have one text field for a date entry, I am trying to split the year, month and day up into three individual entries, seperated by '/'. The original text entry looks like:
<%= f.text_field :date, :placeholder => 'YYYY/MM/DD' %>
I would like to split this into three text_fields, and append them together, and put it into the date entry in the database.
How can I do this?

You often use textfields for dates because you then only needs one field. When you are having three different fields should you consider using select instead. Rails have a the date helper method date_select, so it would be somethink like this:
<%= f.date_select :date %>
This creates one select for years, one for months and one for days.
You can read more on http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html#method-i-date_select

Add three virtual attributes to the model:
attr_accessor :form_month, :form_day, :form_year
Then put the following in the controller, not the model:
def create
# ...
form_date = [ params[:form][:form_month], \
params[:form][:form_day] ,
params[:form][:form_year] ].join("/")
#my_model.date = Date.parse(form_date)
# ... save and return ...
end
It would be a good idea to manually check each form parameter for validity. Date#parse may spit out the incorrect date if fed an incomplete date string.
Date.parse "2005/11/4"
# => Fri, 04 Nov 2005
Date.parse "/11/4"
# => Mon, 04 Nov 2013

Try to use https://github.com/plataformatec/simple_form
<%= f.input :deadline, :start_year => Date.today.year, :end_year => Date.today.year + 1, :order => [:day, :month, :year] %>
Do you have many options.

Related

Rails 4 : wrong time in edit view with string field and TimeZone per request

I develop an application in Ruby on Rails 4 with TimeZone per request.
I want to use a datetime picker (http://xdsoft.net/jqplugins/datetimepicker/) in my application to replace the default Simple_form datetime input (5 combo-boxes...).
For this kind of datetime picker (I search for others, it's the same), in my view, I have to use a "string" field, like this :
<%= f.input :done_at, as: :string, input_html: { :data => { :behaviour => "datetimepicker" } } %>
When I post the form, Rails take care of the timezone and store in the database the time in UTC.
For example, I put "2014-03-14 19:45:07" (local time is Paris, so UTC +0100) in the field, and I have "2014-03-14 18:45:07" in the database, in UTC. It's correct.
But when I want to edit the information, Rails fill in the field with a wrong time. The offset timezone is lost and I have "2014-03-14 18:45:07" in the field (the UTC time), so 1 hour before the correct time.
How can I have the correct time taking care of the user timezone ? (not in UTC)
I tried the solution found on http://jessehouse.com/blog/2013/11/15/working-with-timezones-and-ruby-on-rails/ to override the display of dates, but it doesn't work.
def done_at
super.in_time_zone(time_zone) if super && time_zone
end
If in my view, I put #action.done_at, the time is correct but not in the field.
Thanks,
Fred
Set the value of the input explicitly. You can move #object.done_at.in_time_zone(time_zone) to a helper if you want
<%= f.input :done_at, as: :string, input_html: { :data => { :behaviour => "datetimepicker" }, :value => #object.done_at.in_time_zone(time_zone).strftime('%d-%m-%Y %H:%M') } %>

How to parse time field into hours, minutes and seconds?

How would I parse my start_at column to be three different fields for hours, minutes and seconds instead of just one?
Note: I want to keep it as one column and not make three different ones.
Here is my code:
Table:
class CreateTimers < ActiveRecord::Migration
def change
create_table :timers do |t|
t.time :start_at
t.timestamps
end
end
end
Form:
<%= form_for(#timer) do |f| %>
<div class="form-inputs">
<%= f.text_field :start_at %>
</div>
<div class="form-actions">
<%= f.submit 'Start', :class => 'btn-primary span3' %>
</div>
<% end %>
If you just want to split time to three separate inputs you can use the time_select helper.
Otherwise use the strftime method; Check http://strfti.me for more help.
You don't say what format your time values are in. Because users love to be different, if you give them a free-form text field, odds are really good you'll get data in varying formats, so you'll have to be flexible.
You can try using DateTime's parse method, which provides some flexibility for the formats.
DateTime.parse('2001-02-03T04:05:06+07:00')
#=> #<DateTime: 2001-02-03T04:05:06+07:00 ...>
DateTime.parse('20010203T040506+0700')
#=> #<DateTime: 2001-02-03T04:05:06+07:00 ...>
DateTime.parse('3rd Feb 2001 04:05:06 PM')
#=> #<DateTime: 2001-02-03T16:05:06+00:00 ...>
There's also the Chronic gem, which allows even more flexibility in how the values can be entered.
Once you have a DateTime or Time value, you can use that object's methods to get at the hour, minute and second fields:
now = Time.now # => 2012-10-03 07:50:26 -0700
now.hour # => 7
now.min # => 50
now.sec # => 26
or:
require 'date'
now = DateTime.now # => #<DateTime: 2012-10-03T07:52:53-07:00 ((2456204j,53573s,622304000n),-25200s,2299161j)>
now.hour # => 7
now.min # => 52
now.sec # => 53
Check out strftime at http://www.ruby-doc.org/core-1.9.3/Time.html
For e.g.
start_at - 2011-04-26 04:14:56 UTC
For hours:
start_at.strftime("%H") = "04"
For minutes:
start_at.strftime("%M") = "14"
For seconds:
start_at.strftime("%S") = "26"
start_at.strftime("%H %M %S")
More detailed documentation is at: http://www.ruby-doc.org/core-1.9.3/Time.html

Ruby on rails 3 select box values combined ready for validation, is this possible?

I have created a custom date_Select field using 3 separate select fields:
<%= f.select :day, options_for_select(User::DAYS), :include_blank => "Day:" %>
<%= f.select :month, options_for_select(User::MONTHS), :include_blank => "Month:" %>
<%= f.select :year, options_for_select(User::YEAR_RANGE), :include_blank =>"Year:" %>
In my User.rb (Model) I have this validation rule and also using validates_timelessness gem:
MONTHS = ["January", 1], ["February", 2]..etc
DAYS = ["01", 1], ["02", 2], ["03", 3]..etc
START_YEAR = Time.now.year - 111
END_YEAR = Time.now.year
YEAR_RANGE = START_YEAR..END_YEAR
validates :birthday, :timeliness => {:on_or_before => lambda { Date.current }, :type => :date, :on_or_before_message => "Select a valid birthday"}
I have created some tests which work perfectly fine with the date_select that comes with rails but that date_select is buggy which is why I opted for a custom one. My only issue now is I wish to get day, month and year to work with my :birthday symbol. How do I combine all 3 so that my :birthday symbol can use the select data? If that makes sense...
The date_select would have been perfect but it lets users submit a form without the yea being filled out and if a users chooses 1 for a day and clicks submit it will automatically select january. I haven't found a way round that.
So I'm using 3 separate select fields which I want to combine and make work with :birthday just like date_select did.
Help is appreciated.
i would recommend using a date-select pop-up.
There are several gems available. I have used the one detailed in http://www.rubyinside.com/calendar-date-select-a-lightweight-prototype-based-datetime-picker-for-rails-developers-573.html with success.
Once you have a real date field you'll be in a better position to perform date type validations including ranges and presence that you can be confident in worrectly with that type of data.
The select_date isn't buggy, you need to perform the necessary data validation on your end so an empty year isn't allowed. Validation is designed to let you specify what data is allowed into your app. Only you can be the gatekeeper of what is considered 'valid'.

Rails date_select helper and validation

I have a date field in a model backed form in my Rails App:
<%= f.date_select :birthday,
{:start_year => Time.now.year,
:end_year => 1900,
:use_short_month => true,
:order => [:month, :day, :year],
:prompt => {:month => 'Month', :day => 'Day', :year => 'Year'}},
{:class => 'year',
:id => 'user_birthday'}
%>
It is being validated in the model code using:
validates_presence_of :birthday, :message => 'is a required field'
Unfortunately, if the user enters a partial value such as just the year, the form still submits without an error. Instead a funky date value gets written to the db. How do I make all three fields be mandatory?
I'd like to write a custom validation for this, but I don't know how to properly access the indvidual pieces of the birthday element. How can I do this?
Thanks!
Moe
I think you would have to create the validation in the controller itself.
The date parts are being passed to birthday(1i), birthday(2i) and birthday(3i). The problem here is that they are assigned immediately when passing the attributes and thus before any validations occur.
You could also overwrite the attributes= method to create your own validation there, but I would not suggest you to do that.
Keep in mind that if you do validations, it might be good to validate against any incorrect date as well. (for instance 31st of February, which when passed will yield 2nd of March and not an error).
I think the main issue here is that ActiveRecord is actually replacing the empty values with 1 before creating the Date, which also means that if the visitor pass only the year, the date will be created on the 1st of January that year. I guess that is an expected behaviour to allow use of only one of year/month/day select and still create a useful date.
Related to this post, this is the best solution I've found. However I should add :day, :month, :year as attr_accessible, thing I don't understand why.. (because of validation? please let me know..)
User.rb
MONTHS = ["January", 1], ["February", 2], ...
DAYS = ["01", 1], ["02", 2], ["03", 3], ...
START_YEAR = Time.now.year - 100
END_YEAR = Time.now.year
YEAR_RANGE = START_YEAR..END_YEAR
attr_accessible :day, :month, :year
attr_accessor :day, :month, :year
before_save :prepare_birthday
validate :validate_birthday
private
def prepare_birthday
begin
unless year.blank? # in order to avoid Year like 0000
self.birthday = Date.new(self.year.to_i, self.month.to_i, self.day.to_i)
end
rescue ArgumentError
false
end
end
def validate_birthday
errors.add(:birthday, "Birthday is invalid") unless prepare_birthday
end
user registration form
<%= f.select :month, options_for_select(User::MONTHS), :include_blank => "Month" %>
<%= f.select :day, options_for_select(User::DAYS), :include_blank => "Day" %>
<%= f.select :year, options_for_select(User::YEAR_RANGE), :include_blank =>"Year" %>
You could override the validate_on_create method, like the following:
class MyModel < ActiveRecord::Base
def validate_on_create
Date.parse(birthday)
rescue
errors.add_to_base("Wrong date format")
end
end
After following Benoitr's suggestions I came up with something similar using virtual attributes. On the View side there are 3 separate select's (year,mon,day) inside of a 'fields_for'. The data is submitted to the controller's mass assignment (no modifications in controller, see asciicasts #16) and then passed to a getter/setter (i.e. virtual attribute) in the model. I'm using Rails 3.0.3, and simpleForm for the view code.
In the View:
<%= f.label "Design Date", :class=>"float_left" %>
<%= f.input :design_month, :label => false, :collection => 1..12 %>
<%= f.input :design_day, :label => false, :collection => 1..31 %>
<%= f.input :design_year, :label => false, :collection => 1900..2020 %>
In the Model:
validate :design_date_validator
def design_year
design_date.year
end
def design_month
design_date.month
end
def design_day
design_date.day
end
def design_year=(year)
if year.to_s.blank?
#design_date_errors = true
else
self.design_date = Date.new(year.to_i,design_date.month,design_date.day)
end
end
def design_month=(month)
if month.to_s.blank?
#design_date_errors = true
else
self.design_date = Date.new(design_date.year,month.to_i,design_date.day)
end
end
def design_day=(day)
if day.to_s.blank?
#design_date_errors = true
else
self.design_date = Date.new(design_date.year,design_date.month,day.to_i)
end
end
#validator
def design_date_validator
if #design_date_errors
errors.add(:base, "Design Date Is invalid")
end
end
'design_date_attr' is the virtual attribute which sets the value of design_date in the database. The getter passes back an hash similar to what gets submitted in the form. The setter checks for blanks and creates a new date object and sets it and also sets the error variable. The custom validator :design_date_validator checks for the error instance variable and sets the errors variable. I used ':base' because the variable name was not human readable and using base removes that value from the error string.
A few things to refactor might be the error checking instance variable, but it seems to work at least. If anyone knows a better way to update the Date objects I'd love to hear it.

Scaffolding A model with an attribute of type datetime creates a 10 years range in the form

For a simple rails application ( 1.86 /2.3.5) , lets say I run a simple scaffold
script/generate scaffold blog title:string content:text published:date
When I open up the new / edit view for the blog controller in index/new.html.erb , I see that the drop down enabler for date select has a date range of 2005 - 2015 , i.e 5 years +/-
I tried to change this default behavior by introducing this code
f.date_select :entered,
:start_year => 1970,
:end_year => 2020
Apparently this has no impact to the behavior mentioned above. How do I increase the date_select range which seems to be default?
This seems to be ok. Except did you put it inside the <%= %> ?
Edit: Also i noticed that you say your scaffold has published column whereas your date_select refers to the entered column.
As in, <%= f.date_select :published, :start_year => 1970, :end_year => 2020 %>
If that doesn't work you can also try,
<%= f.date_select :published, :start_year => Time.now.year - 40, :end_year => Time.now.year + 10 %>
I tried it and it works for me. Cheers! :)

Resources