Skip to content

Instantly share code, notes, and snippets.

View clm-a's full-sized avatar

Clément Alexandre clm-a

View GitHub Profile
@clm-a
clm-a / Javascript shuffle.js
Created April 1, 2011 14:20
Shuffle a javascript array
Array.prototype.shuffle = function( b ) {
var i = this.length, j, t;
while( i ) {
j = Math.floor( ( i-- ) * Math.random() );
t = b && typeof this[i].shuffle!=='undefined' ? this[i].shuffle() : this[i];
this[i] = this[j];
this[j] = t;
}
return this;
};
@clm-a
clm-a / index-with-fields_for.html.erb
Created March 16, 2011 09:44
Howto : work with index in Rails 3's fields_for
<%= form_for parent do |parent_form_builder| %>
<%= parent_form_builder.text_field :name %>
<% parent.children.each_with_index do |child, index| %>
<% parent_form_builder.fields_for :children, child do |child_form_builder| %>
<%= child_form_builder.select :age, (0..99).to_a %>
<%# generates "parent[:children_attributes][index][age]" as name for the input %>
<% end %>
<% end %>
<%= f.submit %>
<% end %>
# Find me at https://gist.github.com/863277
# Run me with "rails new myapp -J -m https://gist.github.com/raw/863277/rails3-template-with-devise-compass-and-friends.rb"
# You should visit
# http://everydayrails.com/2011/02/28/rails-3-application-templates.html
# http://railswizard.org
# http://stackoverflow.com/questions/4999207/rails-3-application-templates
# https://github.com/fnichol/rails-template-recipes
# for some inspiration about application templates.
# twitter.com/@clmntlxndr