Register now and start sharing your code snippets.

Time ago in words (minutes, hours, days, weeks, months ago in words)

Ruby posted about 1 month ago by christian

   1  def minutes_in_words(timestamp)
   2      minutes = (((Time.now - timestamp).abs)/60).round
   3      
   4      return nil if minutes < 0
   5      
   6      case minutes
   7        when 0..4            then '&lt; 5 minutes'
   8        when 5..14           then '&lt; 15 minutes'
   9        when 15..29          then '&lt; 30 minutes'
  10        when 30..59          then '&gt; 30 minutes'
  11        when 60..119         then '&gt; 1 hour'
  12        when 120..239        then '&gt; 2 hours'
  13        when 240..479        then '&gt; 4 hours'
  14        when 480..719        then '&gt; 8 hours'
  15        when 720..1439       then '&gt; 12 hours'
  16        when 1440..11519     then '&gt; ' << pluralize((minutes/1440).floor, 'day')
  17        when 11520..43199    then '&gt; ' << pluralize((minutes/11520).floor, 'week')
  18        when 43200..525599   then '&gt; ' << pluralize((minutes/43200).floor, 'month')  
  19        else                      '&gt; ' << pluralize((minutes/525600).floor, 'år')
  20      end
  21    end

There are also similar implementations:
  1. http://www.actsasflinn.com/articles/2007/04/10/time-ago-method-for-ruby-on-rails
  2. http://timeago.yarp.com/
Tagged month, week, hour, minutes, words, rails, ruby

How to use named_scope in Rails

Ruby posted 2 months ago by christian

Simple example of how to use the named_scope feature:

   1  class Feed < ActiveRecord::Base
   2   
   3    named_scope :active, :conditions => "(active = 1)"
   4    named_scope :stale,  :conditions => ["last_updated > ?", 30.minutes.ago.to_s(:db)]

Usage:

   1  Feed.active # return the active feeds

Chaining is also possible:

   1  Feed.active.stale # return the feeds that need to be updated

Tagged named_scope, rails, activerecord, ruby

Showing ActiveRecord error messages from jQuery Ajax actions and scripts

HTML (Rails) posted 3 months ago by christian

The HTML , in a layout file, for example application.html.erb:

   1  <div id="error-message" style="display:none">
   2  </div>

The JavaScript, rendered by for example create.js.erb:

   1  <% if !@category.valid? %>
   2  <%
   3    errors = <<ERR
   4    <p>Please fix the following errors:</p>
   5    <ul>
   6      #{@category.errors.collect{|err| "<li>" + err[0] + " " + err[1] + "</li>" } }
   7    </ul>
   8  ERR
   9  %>
  10  $('#error-message').html('<%= escape_javascript(errors) %>');
  11  $('#error-message').show();
  12  <% else %>
  13  $('#error-message').hide();
  14  <% end %>

Tagged ajax, error, jquery, rails

How to use jQuery with Rails 2.0 - aka How to fix "ActionController::InvalidAuthenticityToken (ActionController::InvalidAuthenticityToken)"

Ruby posted 3 months ago by christian

All credit goes to Henrik Nyh for writing a blog post about how to fix this issue.

This is a slight variation of his code:

In application.html.erb, or whatever layout file you’re using, put:

   1  <%= javascript_tag "window.AUTH_TOKEN = '#{form_authenticity_token}';" %>

In application.js, or whatever JavaScript file you’re using, put:

   1  $(document).ajaxSend(function(event, request, settings) {
   2    if (typeof(window.AUTH_TOKEN) == "undefined") return;
   3    settings.data = settings.data || "";
   4    settings.data += (settings.data ? "&" : "") + "authenticity_token=" + encodeURIComponent(window.AUTH_TOKEN);
   5  });

That’s all…

Tagged authenticity, rails, rails 2, token, jquery, javascript, ajax

ActionController::InvalidAuthenticityToken (ActionController::InvalidAuthenticityToken):

HTML (Rails) posted 3 months ago by christian

   1  <input name="authenticity_token" value="<%= form_authenticity_token %>" type="hidden" />

or

   1  <%= token_tag %> 

Tagged authenticity, token, ruby, rails