in Projects, Programming, Technology

Ruby on Rails Cheat Sheet

Here are some of my actual Google searches from the last few days with the code fragments I was looking for.

rails format number with commas

number_to_currency(1234567890.506, precision: 3)

rails date from datetime

DateTime.new.to_date

ruby is numeric

12.is_a? Numeric

ruby 1 month from now

1.month.from_now

ruby hash delete key

{a: 1}.delete(:a)

ruby hash key order

Keys are preserved in the order inserted.

savon gyoku unwrap

Savon.client(unwrap: true)

rails before_action admin

before_filter do 
  redirect_to root unless current_user.admin?
end

rails controller require admin

before_filter do 
  redirect_to root unless current_user.admin?
end

rails routes require admin

Routes are created before the request is handled, so you can’t dynamically create routes based on the user.

rails route require user

authenticate :user do
   get 'dashboard' => 'dashboard#show'
   ...

rails devise admin role

class AddAdminToUsers < ActiveRecord::Migration
 def change
   add_column :users, :admin, :boolean, default: false
 end
end
if current_user.admin?
  # do something
end

nokogiri pretty print

Nokogiri::XML('<myxml/>', &:noblanks)

rails pretty print xml

Nokogiri::XML('<myxml/>', &:noblanks)

ruby strftime

Time.now.strftime('%a, %b %-d %H:%M:%S')

rails migration rename table

rename_table :bank_settings, :bank_accounts

Write a Comment

Comment