How do you do relative time in Rails?
Sounds like you’re looking for the time_ago_in_words method (or distance_of_time_in_words), from ActiveSupport. Call it like this: <%= time_ago_in_words(timestamp) %>
Sounds like you’re looking for the time_ago_in_words method (or distance_of_time_in_words), from ActiveSupport. Call it like this: <%= time_ago_in_words(timestamp) %>
require ‘csv’ csv_text = File.read(‘…’) csv = CSV.parse(csv_text, :headers => true) csv.each do |row| Moulding.create!(row.to_hash) end
The code date.to_time.to_i should work fine. The Rails console session below shows an example: >> Date.new(2009,11,26).to_time => Thu Nov 26 00:00:00 -0800 2009 >> Date.new(2009,11,26).to_time.to_i => 1259222400 >> Time.at(1259222400) => Thu Nov 26 00:00:00 -0800 2009 Note that the intermediate DateTime object is in local time, so the timestamp might be several hours off from … Read more
Try this: require ‘open-uri’ open(‘image.png’, ‘wb’) do |file| file << open(‘http://example.com/image.png’).read end
Try this: arr = [5, 6, 7, 8] arr.inject{ |sum, el| sum + el }.to_f / arr.size => 6.5 Note the .to_f, which you’ll want for avoiding any problems from integer division. You can also do: arr = [5, 6, 7, 8] arr.inject(0.0) { |sum, el| sum + el } / arr.size => 6.5 You … Read more
The & marks an alias for the node (in your example &default aliases the development node as “default”) and the * references the aliased node with the name “default”. The <<: inserts the content of that node. Allow me to quote the YAML spec here: Repeated nodes (objects) are first identified by an anchor (marked … Read more
According to the docs, #Rails.env wraps RAILS_ENV: # File vendor/rails/railties/lib/initializer.rb, line 55 def env @_env ||= ActiveSupport::StringInquirer.new(RAILS_ENV) end But, look at specifically how it’s wrapped, using ActiveSupport::StringInquirer: Wrapping a string in this class gives you a prettier way to test for equality. The value returned by Rails.env is wrapped in a StringInquirer object so instead … Read more
Try this way: <%= f.select(:object_field, [‘Item 1’, …], {}, { :class => ‘my_style_class’ }) %> select helper takes two options hashes, one for select, and the second for html options. So all you need is to give default empty options as first param after list of items and then add your class to html_options. http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-select
controller_name holds the name of the controller used to serve the current view.
You want the number_with_delimiter method. For example: <%= number_with_delimiter(@number, :delimiter => ‘,’) %> Alternatively, you can use the number_with_precision method to ensure that the number is always displayed with two decimal places of precision: <%= number_with_precision(@number, :precision => 2, :delimiter => ‘,’) %>