Flowdock

Recent notes

RSS feed
May 7, 2012
0 thanks

Hidden Field Example

Here’s a pseudo code example of a hidden field within an ERB template. A post has many comments and this comment form is in a post’s show view. This would set a comment’s post_id attribute.


<%= form_for(@comment) do |f| %>

<%= f.hidden_field :post_id, :value => @post.id %>

<% end %>


May 3, 2012
0 thanks

Starts with a Capital Letter

(or any regular expression you’d like)

'Abracadabra'[0..0] =~ /[A-Z]/       # => true
April 30, 2012 - (>= v3.1.0)
0 thanks

method to use instead

This may be obvious, but the replacement for this method is csrf_meta_tags.

April 27, 2012 - (>= v3.2.1)
1 thank

Example from Code School

module Tweets

 class ShowPresenter
   extend ActiveSupport::Memoizable

   def initialize(tweet)
     @tweet = tweet
   end

   def username
     @tweet.user.username
   end

   def status
     @tweet.status
   end

   def favorites_count
     @tweet.favorites.count
   end

   memoize :username, :status, :favorites_count

 end
end

From http://railsbest.com/

April 26, 2012 - (v1_9_3_125)
0 thanks

Dir

The documentation is using File.directory?(“/path/to/directory”), but the method being referred to is Dir.exists?().

Dir.exists?(".")
 => true

The source code is the same as File.directory?().

April 26, 2012
2 thanks
April 25, 2012 - (>= v3.1.0)
6 thanks

HTML5 data- attributes using RESTful approach

HTML5 specifies extensible attributes like data-foo=“bar” (or as in Twitter Bootstrap data-toggle=“modal”), which poses two problems for Rails.

First, if you’re using symbol notation in link_to to specify attributes, this fails (dash is not a valid symbol character), so

Invalid!

link_to "Edit", @user, :class => "btn", :data-toggle => "modal"

There are two solutions:

  1. put the symbols in quotes,

  2. use the special :data hash

Solution 1: Quote Symbols

link_to "Edit", @user, :class => "btn", "data-toggle" => "modal"

Solution 2: Use the :data hash

link_to "Edit", @user, :class => "btn", :data => {:toggle => "modal"}

Resulting HTML

<a href="/users/1" class="btn", data-toggle="modal">Edit</a>

The second is minimally documented, but as a hash, can accept multiple values and is perhaps a little cleaner

April 24, 2012 - (v1_9_3_125)
0 thanks

BasicObject.new

Instantiates a new blank object (devoid of methods). The only class method of class BasicObject. see “ri BasicObject”

eg,

> o=BasicObject.new (Object doesn’t support #inspect)

>

> o.methods NoMethodError: undefined method `methods’ for #<BasicObject:0x0000000267a0a0>

> def o.to_s > self > end

> nil

> o

> #<BasicObject:0x0000000267a0a0>

April 22, 2012
0 thanks

I got it

Perfect match to work with attr_accessible

April 19, 2012 - (v2.3.2 - v3.2.1)
0 thanks

Locale

To change default locale by the parameter you can set :locale option, like below:

select_date 'user', 'birth', :locale => 'de'
April 19, 2012 - (v3.1.0 - v3.2.1)
0 thanks

Missed close tag

At the page http://apidock.com/rails/ActionView/Helpers/TagHelper/tag

<tt>.data()</tt> should be instead of <tt>.data()<tt>

April 9, 2012
0 thanks

ferragamo

Salvatore <a href=“http://www.ferragamochaussures.com”>ferragamo pas cher, 30 ~ 70% de rabais! Salvatore ferragamo chaussures!Bienvenue à notre Salvatore ferragamo en ligne, la spécialité de vendre de Salvatore ferragamo Pas Cher style. inclure:ferragamo chaussures,ferragamo sac etc.Nous sommes à votre bon choix.site web:<a href=“http://www.ferragamochaussures.com”>http://www.ferragamochaussures.com/

April 3, 2012
1 thank

Looks like this method has trouble with attributes:

ex:

require 'rubygems'
require 'bundler'
require 'active_support/core_ext'
require 'pp'

xml  = 
  '<test id="appears">
    <comment id="doesnt appear">
      it worked
    </comment>
    <comment>
     see!
    </comment>
    <comment />
  </test>'

hash = Hash.from_xml(xml)

pp hash

#=>{"test"=>{"id"=>"appears", "comment"=>["it worked", "see!", nil]}}

# Notice how the id attribute on the first comment element doesn't appear.
April 2, 2012
1 thank

Options select_hour

In my view I wanted to do this <%= select_hour(@hour, :start => 8, :end => 12) %> but did not work. I looked at the documentation and have not seen anything like it. http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html#method-i-select_hour

So I studied how it worked this helper. http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html#method-i-select_hour develop and achieve this:

the helper application:

module DataAnnouncementsHelper

class HelperDate < ActionView::Helpers::DateTimeSelector
  def select_hour
    if @options[:use_hidden] || @options[:discard_hour]
      build_hidden(:hour, hour)
    else
      build_options_and_select(:hour, hour, :end => @options[:end], :start => @options[:start], :ampm => @options[:ampm])
    end
  end
end

def select_hour(datetime, options = {}, html_options = {})
   HelperData.new(datetime, options, html_options).select_hour
end

end

The view:

<%= select_hour(@hour, :start => 8, :end => 12) %>

Where @hour = 10 the result is:

<select id=“date_hour” name=“date[hour]”>

<option value="08">08</option>
<option value="09">09</option>
<option value="10" selected="selected">10</option>
<option value="11">11</option>
<option value="12">12</option>

</select>

March 29, 2012 - (v2.3.2 - v3.2.1)
1 thank

JQuery script for dynamically adding and removing fields_for

I like drogus idea. But I wanted a cleaner one, so I created an unobtrusive JQuery script to have the same functionality.

Example Usage:

<%= form_for @post do |form| %>
  Title: <%= form.text_field :title %>
  Body: <%= form.text_field :body %>

  Tags:
  <div id="tag-list"></div>

  <div class="numerous">
    <div class="numerous-form">
      <%= form.fields_for :tag, Tag.new, :child_index => "replace_this" do |f| %>
        <%= f.text_field :name %>
        <%= f.hidden_field :_destroy, :value => 0, :class => "numerous-remove-field" %>
        <%= link_to "delete", "#", :class => "numerous-remove" %>
      <% end %>
    </div>

    <%= link_to "add tag", "#", :class => "numerous-add", :id => "for-tag-list" %>
  </div>
<% end %>

See script at: http://github.com/kbparagua/numerous.js

March 28, 2012 - (>= v3.0.0)
0 thanks

:as option

Code

root :to => 'projects#index', :as => 'foobar'

will generate a helper

foobar_path
March 28, 2012
0 thanks

Disable layout on ajax

In actions that may or may not be loaded via ajax I use:

render :layout => !request.xhr?

For an entire controller I might use something like:

layout :has_layout?

private
  def has_layout?
    request.xhr? ? false : controller_name
  end

What seems unusual is that

layout true

will try look for the layout true.erb

March 27, 2012 - (v3.1.0 - v3.2.1)
2 thanks
March 27, 2012 - (v3.1.0 - v3.2.1)
1 thank
March 27, 2012
3 thanks

Makes it possible to use a scope through an association

This is a very useful method if you want to to use a scope through an association:

class Book < ActiveRecord::Base
  scope :available, where(:available => true)
end

class Author < ActiveRecord::Base
  has_many :books
  scope :with_available_books, joins(:books).merge(Book.available)
end

# Return all authors with at least one available book:
Author.with_available_books

See http://asciicasts.com/episodes/215-advanced-queries-in-rails-3 for more info.

March 27, 2012
2 thanks

Reorder

If you want to override previously set order (even through default_scope), use reorder() instead.

E.g.

User.order('id ASC').reorder('name DESC')

would ignore ordering by id completely

March 27, 2012 - (>= v3.2.1)
0 thanks

Example to auto download

Controller

op = Operation.find(params[:id])
fname = "operation_#{op.id}_#{DateTime.now.to_i}.csv"
send_data op.export(params[:url_type]), 
  :type => 'text/csv; charset=iso-8859-1; header=present',
  :disposition => "attachment; filename=#{fname}.csv"

export_csv

def export(url_type)
  csv_data = CSV.generate do |csv|
    csv << self.header_columns # simple array ["id","name"]
    url_items = @operation.url_items.where(:url_type => url_type)
    url_items.each do |url_item|
      csv << self.process_row(url_item)  # simple array [1,"bob"]
    end 
  end 
  return csv_data
end
March 26, 2012 - (v2.3.2 - v3.1.0)
1 thank

Use exist scopes on default_scope - pay attention

To use exists scopes on default_scope , you can use something like:

class Article < ActiveRecord::Base
  scope :active, proc {
    where("expires_at IS NULL or expires_at > '#{Time.now}'")
  }

  scope :by_newest, order("created_at DESC")

  default_scope by_newest
end

But, if you would add a filter, and it require a lazy evaluate, use block on default_scope declaration, like:

default_scope { active.by_newest }
March 22, 2012 - (>= v3.1.0)
0 thanks

Deprecation in 3.1+

In Rails 3.1 and higher, just use ruby’s SecureRandom, e.g.

Before

ActiveSupport::SecureRandom.hex

After

SecureRandom.hex
March 18, 2012
0 thanks

Use Join to Turn Array Items into a String.

If you’re looking to take an array like

[ 'don', 'draper' ]

And get

'don draper'

Then use join instead:

[ 'don', 'draper' ].join( ' ' ) 

#=> 'don draper'
March 18, 2012
0 thanks

Destructive to the Original String.

Just as an FYI this function is destructive to the original String object.

name = 'draper' #=> "draper"

name.insert( 0, 'don ' ) #=> 'don draper'

name #=> 'don draper'
March 15, 2012 - (>= v3.2.1)
0 thanks

Deprecated proxy_owner

Just change your

proxy_owner

calls to

@association.owner

Found it here: http://mileszs.com/deprecation-warnings-for-proxyowner-in-rails