Rails: Human-friendly date format for text_field - ruby-on-rails

I have a birth_date field that is stored as a datetime value. The default rails form helpers spit out a not-too-friendly format, e.g. "2008-06-10 22:33:19.000000". The below is the vanilla rails way.
<div class="field">
<%= f.label :birth_date %>
<%= f.text_field :birth_date, :size=>"20" %>
</div>
How can I simply apply a format? I tried various approaches, for example strftime should work, I thought. But when I try the following, I get an error undefined method 'strftime' for nil:NilClass
<div class="field">
<%= f.label :birth_date %>
<%= f.text_field :birth_date, :value=>f.object.birth_date.strftime('%m/%d/%Y'), :size=>"20" %>
</div>
Based on some other questions/answers, I tried the following. It works for non-null values, but it is ugly code, and it doesn't work for blank values (actually it shows today's date).
<div class="field">
<%= f.label :birth_date %>
<%= f.text_field :birth_date, :value=> Time.parse(f.object.birth_date.to_s).strftime('%m/%d/%Y'), :size=>"20" %>
</div>
In playing around, it seems that outputting f.object.birth_date is treated as a date, rather than datetime. However, when displayed in the text_field (original, ugly formatting), it includes the time. It is friday afternoon, and my combined lack of familiarity with rails forms and ruby date/time objects is making me feel foolish.
Any simple way to get my form to display nothing if null/blank, and a friendly date otherwise?

If you want a blank string if birth_date is empty, you should simply check that birth_date is non-nil beforehand:
<%= f.text_field :birth_date, :value => (f.object.birth_date.strftime('%m/%d/%Y') if f.object.birth_date), :size => "20" %>
This way, when birth_date is nil, :value gets set to nil.

Have you tried using the date_select form builder helper method? You can also use datetime_select, but it looks like you just want to work with the date here.
<div class="field">
<%= f.label :birth_date %>
<%= f.date_select :birth_date %>
</div>
The API docs are here: http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html#method-i-date_select
Add on the options you need.
I've always used that approach, or a javascript date selector like this one from jQuery UI. There are plugins for most JS frameworks these days.
If it MUST be a text_field, use a human language date parsing library like chronic. It'll work, but will require that you parse the input from the form somewhere before applying the attribute to your object.

Related

f.time_field with 24 hour clock - Rails Fields

I've got a little Rails app that I need to use the 24-hour clock for inputting times. Right now, I've got:
<div class=".col-md-7">
<%= f.label :backinservice %><br>
<%= f.time_field :backinservice %>
</div>
Which gives an AM/PM option. When I input a time like 18:56, it automagically converts it to 06:56 PM. Normally that would be totally ok, just not in this app.
I also tried:
<div class=".col-md-7">
<%= f.label :recieved %><br>
<%= f.time_field :recieved, :format=>"%H:%M" %>
</div>
But that doesn't work either.
Is there a :format option that allows for straight use of the 24-hour clock?
You have to pass the format using value key.
<div class=".col-md-7">
<%= f.label :recieved %><br>
<%=
f.time_field :recieved,
value: "%H:%M",
min: 'hh:mm:ss',
max: 'hh:mm:ss'
%>
</div>
The doc says:
The default value is generated by trying to call strftime with “%T.%L” on the objects's value. It is still possible to override that by passing the “value” option.
I had to do the following to get this to work for me:
<%=
f.time_field :my_time_field,
value: f.object.my_time_field.strftime("%H:%M")
%>
Found this out by looking at the definition of time_field, that is only creating a new instance of Tags::TimeField.new
In turn Tags::TimeField.new only does one thing which is to define the format.

Submitting default value in text form Ruby on Rails

I'm developing a simple rails app where I want to plot some stock charts. The problem is that when I start my server and load localhost the default value/ticker symbol is not loading which means that I have to type in a ticker in my form for it to work.
I found this thread where I learnt how to write a default value in my form/view, like so:
<%= form_for :find_it do |f| %>
Ticker symbol: <%= f.text_field :string, :value => "JPM" %></br>
<%= f.submit "Find" %>
<% end %>
and that's all fine, but it does not submit the value by default.
So how do I go about fixing this and what is the best practice?
In your input field you have list your attribute as a string, while that is the type, it most likely isn't the actual name of the attribute you wish to save "JPM". So you should change
<%= f.text_field :string, :value => "JPM" %>
to
<%= f.text_field :attribute_name, :value => "JPM" %>
If I copy and paste your form into a Rails app on my machine it does display a text field populated with "JPM", which I believe is correct.
When you hit submit the form will post to a create action with params containing:
"find_it"=>{
"string"=>"JPM"
}
Another thing I noticed is that you have f.text_field :string. This should be the name of your attribute, rather than the type (i'm assuming that you don't have a field called string).

Ruby on rails cant get values from textfield to array

I need to get an array of strings from f.text_field and store it into a database.
<div>
<%= f.label :name %>
<%= f.text_field :name %>
</div>
<div>
<%= f.label :tags %>
<%= f.text_field :tags%>
</div>
tags is an attribute and it is in an array. Name stores into the database correctly but nothing to tags.
How can i do this?
Thanks
You'll probably want to either parse that on the client side with JS to create an array that gets stored in a hidden field, or something similar. Alternatively, you should pass the raw text to the controller and parse it ruby. You should probably do the latter as you will probably want to sanitize the input.
There is a very good gem available for handling tagging. It's called acts-as-taggable and is found here https://github.com/mbleigh/acts-as-taggable-on

How do you allow a null date entry with ruby on rails?

I'm brand new to ROR. I'm having a pretty simple problem that I can't find the solution to anywhere. In one of my projects I have a field to enter the date. The problem is that for lots of the data that I gather the date is missing. I don't know how to make that null. My form.html looks like this.
<div class="field">
<%= f.label :date %><br />
<%= f.date_select :date %>
</div>
I imagine that just need to add something to this to allow there to be no date entered. Does anyone know how to allow null entries in a date field?
Thanks
You can use the following option to allow blanks if that's what you mean:
<%= f.date_select :date, :include_blank => true %>
You can then leave it blank, so it becomes NULL in your DB.

Rails and date_select

I am trying to use date_select with some of my forms.
<div class="field">
<%= f.label :begin_date %>
<br/>
<%= f.date_select(:begin_date,:start_year => Time.now.year) %>
</div>
Reading through the documentation, I was expecting to get three select tags. What I got was: begin_date(1i), begin_date(2i), begin_date(3i)
If I include these in my model:
attr_accessible :begin_date(1i), :begin_date(2i), :begin_date(3i),
The IDE does not like it. What am I doing wrong? Also, is there a date select method that would return the values in a single tag, instead of 3?
You can just do attr_accessible :begin_date, and it will stitch together the date components into a single Date value for you.

Resources