Posted By

lfatr on 02/16/10


Tagged


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

t9md


RUBY ON RAILS STUFF


 / Published in: Rails
 

  1. A cheat sheet!
  2. http://blog.nanorails.com/pages/rails-1-1-cheat-sheet
  3.  
  4.  
  5. String:
  6. quote = 'quotes are pretty basic but need to be lowercase'
  7.  
  8. Symbols:
  9. :hamster
  10. :cow
  11. :guineapig
  12.  
  13. Constants:
  14. EmpireStateBuilding = "350 5th Avenue, NYC, NY"
  15.  
  16. Methods:
  17. front_door.open
  18. front_door.open.close
  19. front_door.is_open?
  20. front_door.paint( 3, :red )
  21. front_door.paint( 3, :red ).dry( 30 ).close()
  22.  
  23. Class Methods:
  24. Door::new( :oak )
  25.  
  26. Global Variables:
  27. $x, $1, $chunky and $CHunKY_bACOn are examples.
  28.  
  29. Instance Variables:
  30. @x, @y
  31.  
  32. Class variables:
  33. @@x, @@y
  34.  
  35. Blocks:
  36. 2.times { print "Yes, I've used chunky bacon in my examples, but never again!" }
  37.  
  38. Ranges:
  39. (1..3) is a range, representing the numbers 1 through 3.
  40. ('a'..'z') is a range, representing a lowercase alphabet.
  41. (0...5) represents the numbers 0 through 4.
  42.  
  43. Arrays:
  44. [1, 2, 3] is an array of numbers.
  45. ['coat', 'mittens', 'snowboard'] is an array of strings.
  46.  
  47. Hashes:
  48. {
  49. 'name' => 'Peter',
  50. 'profession' => 'lion tamer',
  51. 'great love' => 'flannel'
  52. }
  53.  
  54.  
  55. Emptyness:
  56. plastic_cup = nil
  57. plastic_cup = false
  58.  
  59. IF and UNLESS:
  60. if plastic_cup
  61. print "Plastic cup is on the up 'n' up!"
  62. end
  63.  
  64. unless plastic_cup
  65. print "Plastic cup is on the down low."
  66. end
  67.  
  68. <%= link_to 'host', item.from if item.from != "" -%>
  69.  
  70. print "Yeah, plastic cup is up again!" if plastic_cup
  71. print "Hardly. It's down." unless plastic_cup
  72.  
  73. print "We're using plastic 'cause we don't have glass." if plastic_cup unless glass_cup
  74. **WHEY**
  75.  
  76. leading zeroes:
  77. "%05d" % 200
  78. gives "00200"
  79.  
  80. assigning dependent on...
  81. at_hotel = true
  82. email = if at_hotel
  83. "[email protected]"
  84. else
  85. "[email protected]"
  86. end
  87.  
  88. --- looping through an array with a counter
  89. <% @items.each_with_index do |item, index| %>
  90. <%= index+1 %>.
  91. <%= item.name %>
  92. <% end %>
  93.  
  94. --- ANY ? ---
  95. @sections.any? { |section| section.is_a?(String) }
  96.  
  97. --- IS AN ARRAY ? ---
  98. @sections.is_a?(Array)
  99.  
  100. --- forms ---
  101. <%= f.hidden_field :has_manufacture, :value => "No" %>
  102.  
  103. --- count() ----
  104. nav_items = navItems.size
  105. nav_items = navItems.length
  106.  
  107. --- if / else --
  108. if x == y
  109. beeroclock = 'now'
  110. elsif
  111. beeroclock = 'later'
  112. else
  113. beeroclock = 'over'
  114. end
  115.  
  116.  
  117. --- logging ---
  118. logger.warn "Failed login for '#{params[:email]}' from #{request.remote_ip} at #{Time.now.utc}"
  119.  
  120. ---- sorting and eaching ----
  121. <% @posts = Post.find(:all, :order => 'created_at DESC') %>
  122. <% @posts.each do |post| %>
  123. something
  124. <% end %>
  125.  
  126. ---- more find examples ----
  127. @items = @user.items.find(:all, :order => 'id DESC').paginate(:per_page => 100, :page => params[:page])
  128.  
  129. ---- STRING MANIPULATION ----
  130. truncate(neighbourhood.description, :length => 260)
  131. pluralize(post.responses.size,'response')
  132.  
  133. ----
  134. to_formatted_s
  135. to_param
  136. to_query
  137. to_sentence
  138. to_xml
  139.  
  140. http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/Array/Conversions.html#M000274
  141.  
  142. --- SCRIPT ---
  143. script/server - start server
  144. script/console - run console with all models...
  145. script/server -p 3001 -d - run server on port 3001 and detatch the console
  146. - ( use ps aux & kill to stop it )
  147. script/server -p 3001 -e production - run server in production & on port 3001
  148.  
  149. --- DEBUGGING ---
  150. In A View
  151.  
  152. <%= debug @object %>
  153. <%= debug 'string' %>
  154. <%= 'string'.inspect %>
  155.  
  156. In A Controller
  157.  
  158. logger.debug "The object is #{@object}"
  159. RAILS_DEFAULT_LOGGER.debug @object
  160.  
  161. ---- RAKE ----
  162. rake -T ( is the shit )
  163.  
  164. install, unpack and build gems in a rails project:
  165. rake gems:install
  166. rake gems:unpack
  167. rake gems:build
  168. rake routes -- gets you all the routs in your project
  169.  
  170. point to -> production
  171. rake db:migrate RAILS_ENV=production
  172.  
  173. clear ruby on rails cache
  174. rake tmp:cache:clear
  175.  
  176. generate:
  177. script/generate migration MyNewMigration
  178. script/generate controller static
  179.  
  180. ---- Mongrel ----
  181. STOP MONGREL --daemon
  182. http://www.cyberciti.biz/faq/kill-process-in-linux-or-terminate-a-process-in-unix-or-linux-systems/
  183.  
  184. -- kill deamon --
  185. ps aux (to get the id)
  186. kill id
  187.  
  188. -- SNIPS --
  189. <%= link_to "#{image_tag("go_back.png")}Go Back", user_path(@user) %>
  190.  
  191.  
  192. FINDERS ----------------------------
  193. oldEntries = Posts.find( :all, :conditions => ['visible = ? and created_at <= ?', 'yes', (Time.now - daysToKeep.days)])
  194.  
  195. SCOPES -----------------------------
  196.  
  197. Inside the model:
  198. default_scope :order => 'name ASC'
  199. Entry.named_scope :lastweek, :conditions => ["created_at > ?", 7.day.ago]
  200. Entry.named_scope :isvisible, :conditions => {:visible => 'yes'}
  201.  
  202. -- lambda makes sure the current date is uses and not a cached? one
  203. Entry.named_scope :lastweek, lambda {
  204. {:conditions => ["created_at > ?", 7.day.ago]}
  205. }
  206.  
  207. -- Entries with AND ( headline AND blurb need to be 'bmx' )
  208. Entry.scope_procedure :awesome, lambda { headline_like("bmx").blurb_like("bmx") }
  209.  
  210. -- Entries with OR ( headline OR blurb need to be 'bmx' )
  211. Entry.scope_procedure :awesome, lambda { headline_like_or_blurb_like("bmx") }
  212.  
  213. Entry.named_scope :with_recent_orders, :conditions => ["comments.created_at >= ?", 1.week.ago]
  214.  
  215. RENDER --------------------------
  216. render :partial => "person", :locals => { :name => "david" }
  217. render :partial => "person", :object => @new_person
  218. render :partial => "person", :collection => @winners
  219. render :partial => "admin_person", :collection => @winners, :as => :person
  220. render :partial => "person", :collection => @winners, :spacer_template => "person_divider"
  221. render :partial => "shared/note", :collection => @new_notes
  222. render :partial => "broken", :status => 500
  223. more: http://apidock.com/rails/ActionController/Base/render

Report this snippet  

You need to login to post a comment.