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. else
  84. end
  85.  
  86. --- looping through an array with a counter
  87. <% @items.each_with_index do |item, index| %>
  88. <%= index+1 %>.
  89. <%= item.name %>
  90. <% end %>
  91.  
  92. --- ANY ? ---
  93. @sections.any? { |section| section.is_a?(String) }
  94.  
  95. --- IS AN ARRAY ? ---
  96. @sections.is_a?(Array)
  97.  
  98. --- forms ---
  99. <%= f.hidden_field :has_manufacture, :value => "No" %>
  100.  
  101. --- count() ----
  102. nav_items = navItems.size
  103. nav_items = navItems.length
  104.  
  105. --- if / else --
  106. if x == y
  107. beeroclock = 'now'
  108. elsif
  109. beeroclock = 'later'
  110. else
  111. beeroclock = 'over'
  112. end
  113.  
  114.  
  115. --- logging ---
  116. logger.warn "Failed login for '#{params[:email]}' from #{request.remote_ip} at #{Time.now.utc}"
  117.  
  118. ---- sorting and eaching ----
  119. <% @posts = Post.find(:all, :order => 'created_at DESC') %>
  120. <% @posts.each do |post| %>
  121. something
  122. <% end %>
  123.  
  124. ---- more find examples ----
  125. @items = @user.items.find(:all, :order => 'id DESC').paginate(:per_page => 100, :page => params[:page])
  126.  
  127. ---- STRING MANIPULATION ----
  128. truncate(neighbourhood.description, :length => 260)
  129. pluralize(post.responses.size,'response')
  130.  
  131. ----
  132. to_formatted_s
  133. to_param
  134. to_query
  135. to_sentence
  136. to_xml
  137.  
  138. http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/Array/Conversions.html#M000274
  139.  
  140. --- SCRIPT ---
  141. script/server - start server
  142. script/console - run console with all models...
  143. script/server -p 3001 -d - run server on port 3001 and detatch the console
  144. - ( use ps aux & kill to stop it )
  145. script/server -p 3001 -e production - run server in production & on port 3001
  146.  
  147. --- DEBUGGING ---
  148. In A View
  149.  
  150. <%= debug @object %>
  151. <%= debug 'string' %>
  152. <%= 'string'.inspect %>
  153.  
  154. In A Controller
  155.  
  156. logger.debug "The object is #{@object}"
  157. RAILS_DEFAULT_LOGGER.debug @object
  158.  
  159. ---- RAKE ----
  160. rake -T ( is the shit )
  161.  
  162. install, unpack and build gems in a rails project:
  163. rake gems:install
  164. rake gems:unpack
  165. rake gems:build
  166. rake routes -- gets you all the routs in your project
  167.  
  168. point to -> production
  169. rake db:migrate RAILS_ENV=production
  170.  
  171. clear ruby on rails cache
  172. rake tmp:cache:clear
  173.  
  174. generate:
  175. script/generate migration MyNewMigration
  176. script/generate controller static
  177.  
  178. ---- Mongrel ----
  179. STOP MONGREL --daemon
  180. http://www.cyberciti.biz/faq/kill-process-in-linux-or-terminate-a-process-in-unix-or-linux-systems/
  181.  
  182. -- kill deamon --
  183. ps aux (to get the id)
  184. kill id
  185.  
  186. -- SNIPS --
  187. <%= link_to "#{image_tag("go_back.png")}Go Back", user_path(@user) %>
  188.  
  189.  
  190. FINDERS ----------------------------
  191. oldEntries = Posts.find( :all, :conditions => ['visible = ? and created_at <= ?', 'yes', (Time.now - daysToKeep.days)])
  192.  
  193. SCOPES -----------------------------
  194.  
  195. Inside the model:
  196. default_scope :order => 'name ASC'
  197. Entry.named_scope :lastweek, :conditions => ["created_at > ?", 7.day.ago]
  198. Entry.named_scope :isvisible, :conditions => {:visible => 'yes'}
  199.  
  200. -- lambda makes sure the current date is uses and not a cached? one
  201. Entry.named_scope :lastweek, lambda {
  202. {:conditions => ["created_at > ?", 7.day.ago]}
  203. }
  204.  
  205. -- Entries with AND ( headline AND blurb need to be 'bmx' )
  206. Entry.scope_procedure :awesome, lambda { headline_like("bmx").blurb_like("bmx") }
  207.  
  208. -- Entries with OR ( headline OR blurb need to be 'bmx' )
  209. Entry.scope_procedure :awesome, lambda { headline_like_or_blurb_like("bmx") }
  210.  
  211. Entry.named_scope :with_recent_orders, :conditions => ["comments.created_at >= ?", 1.week.ago]
  212.  
  213. RENDER --------------------------
  214. render :partial => "person", :locals => { :name => "david" }
  215. render :partial => "person", :object => @new_person
  216. render :partial => "person", :collection => @winners
  217. render :partial => "admin_person", :collection => @winners, :as => :person
  218. render :partial => "person", :collection => @winners, :spacer_template => "person_divider"
  219. render :partial => "shared/note", :collection => @new_notes
  220. render :partial => "broken", :status => 500
  221. more: http://apidock.com/rails/ActionController/Base/render

Report this snippet  

You need to login to post a comment.