My question is similar to this SO problem, but it does not answer my question:
Rails 3 default datetime format without UTC
I understand that I can add
Time::DATE_FORMATS[:default] = "%Y/%m/%d"
to my environment.rb and it will change my default time format accordingly.
So when I do Time.now.to_s I get the correct format.
However, my question is, how come this does not work with a form_builder. For example:
<%= f.text_field :date %>
will return a full UTC timestamp: 2013-01-25 07:45:21
I am aware that I can do this
<%= f.text_field :date, :value => #post.date.to_s %>
And it will give me the correct format.
But this solution seems hacky to me. Is this really the only way to do it?
I am not 100% sure. But the answer is most likely no. The rails formhelper just gives you whatever value that is in your database.
Here is where the value is retrieved for your reference:
http://api.rubyonrails.org/classes/ActionView/Helpers/InstanceTag.html#method-i-to_input_field_tag
There is no special processing of it, such as localize or to_s, which is where formatting is applied.
If the date looks like a timestamp (2013-01-25 07:45:21 UTC),
You could do something like the following to views you localize timestamps with:
<%= localize(#post.date, :format => :long) %>
According to your problem, you could try using date_select rather than text_field.
<%= f.date_select :date, :order => [:day, :month, :year] %>
Related
I am trying to build a dropdown select input of months using simple_form. However, I am having trouble figuring out where to even begin. Currently, it is a text input area:
<%= f.input :start_month %>
I need to know what arguments to pass in order for this to be a dropdown of all 12 months. It is not important for it to return an integer value for the months but would be ideal in the event I use it for ordering later on.
I am still a beginner with rails and could really use the help on this one. I can provide any extra information necessary.
Edit:
I would like the dropdown to show the month names, not simply numbers.
Here is my solution:
<%= f.input :start_month, collection: (1..12).map{|i| [I18n.t("date.month_names")[i], i]} %>
What about this:
<%= f.input :start_month, :collection => 1..12 %>
or with month names:
<%= f.input :start_month, :collection => ['January','February',...,'December'] %>
<%= f.select :month, Date::MONTHNAMES.compact.each_with_index.collect{|m,i| [m,i]}, prompt: 'Month'
I am loading from database the only row. The data are stored in variable (e.g.) #data.
In view, if I want to display the value got from database, I have to do following:
<% #data.each do |d| %>
<%=d.name %>
<%end%>
And I would like to ask you - exist any better way? I think it's a bit silly for the only row to use loop... I tried something like
<%= #data.name %>
OR
<%= #data.each.name %>
But in both cases I got the error message about bad syntax...
So to my question - is possible to get display data a bit more elegantly?
EDIT: my query: #data = Car.includes(:tyres).where("param1 = ?", params[:param1])
If you've loaded more than one model (row), then a loop is the natural construct for displaying each value. If you're really set on a one-liner, you could use some of Ruby's list comprehensions:
<%= #data.map(&:name).join(" ") -%>
I think that you are loading .all instead of .first.
In your controller,
#data = Data.where(:some => 'condition').first
or
#data = Data.find(params[:id])
I'm trying to use distance_of_time_in_words (Rails 3) on a created_at column of an ActiveRecord object.
But I get
ActiveSupport::TimeWithZone can't be coerced into Fixnum
when I call
distance_of_time_in_words(#user.created_at)
Any ideas?
You need to supply the to_time as well. Assuming you want to know "how long ago from right now":
<%= distance_of_time_in_words(#user.created_at, Time.now) %>
Alternatively, you could just do this:
<%= distance_of_time_in_words_to_now(#user.created_at) %>
The default date format in Ruby is yyyy-mm-dd, but I needed them to be dd/mm/yyyy in the view
I have wrote a date_format file in config/initializers as:
Time::DATE_FORMATS.merge!( :uk_format => '%m/%d/%Y')
This didn't work since I need to call date.to_s(:uk_format) in order to use this format. But since I'm using <%= f.text_field :paymentDate %> to display the date, I'm not sure I can add to_s to this case.
Can someone help me out please.
Edit 1
Actually date.to_s(:uk_format) doesn't work either, how do I use initializer properly.....?
See: Change default Ruby Time format
Adapted my answer linked to above for the Date and format specified:
Since you're using Rails, take advantage of the I18n support: http://guides.rubyonrails.org/i18n.html#adding-date-time-formats
# config/locales/en.yml
en:
date:
formats:
default: "%d/%m/%Y"
Then you can call I18n.l Date.today to get out a formatted date. Or in your view, you can use <%= l #foo.created_at %>.
Try this instead:
Time::DATE_FORMATS.merge!({:db => '%m/%d/%Y', :uk_format => '%m/%d/%Y'})
Date::DATE_FORMATS.merge!({:db => '%m/%d/%Y', :uk_format => '%m/%d/%Y'})
I have a problem with datetime attribute in Rails model.
model:
attr_accessor :from, :to, :via, :datetime
erb template:
<%= f.text_field :from %>
<%= f.text_field :to %>
<%= f.text_field :via %>
<%= f.datetime_select :datetime, :discard_year => true %>
I got:
#datetime(4i)' is not allowed as an instance variable name
On this line: #search_form = SearchForm.new params[:search_form]
What's wrong?
I don't know exactly what's causing the error, but I can tell you that to fix it, you'll need to change the attribute name datetime to, say sent_on (most anything else, really).
Why the error? I'd guess it's because datetime is a data type in SQL, and thus shouldn't be used as a column name, but the error seems to be coming from ruby - either your model or ActionView, so I'm not sure if that supports my theory...
Anyway, hope this helps!
Update: I just tested it, and you can use "datetime" as a column name in SQL (MySQL, at least). There goes that theory - it's a Rails thing, then, I guess...