/ Published in: Rails
Expand |
Embed | Plain Text
A cheat sheet! http://blog.nanorails.com/pages/rails-1-1-cheat-sheet String: quote = 'quotes are pretty basic but need to be lowercase' Symbols: :hamster :cow :guineapig Constants: EmpireStateBuilding = "350 5th Avenue, NYC, NY" Methods: front_door.open front_door.open.close front_door.is_open? front_door.paint( 3, :red ) front_door.paint( 3, :red ).dry( 30 ).close() Class Methods: Door::new( :oak ) Global Variables: $x, $1, $chunky and $CHunKY_bACOn are examples. Instance Variables: @x, @y Class variables: @@x, @@y Blocks: 2.times { print "Yes, I've used chunky bacon in my examples, but never again!" } Ranges: (1..3) is a range, representing the numbers 1 through 3. ('a'..'z') is a range, representing a lowercase alphabet. (0...5) represents the numbers 0 through 4. Arrays: [1, 2, 3] is an array of numbers. ['coat', 'mittens', 'snowboard'] is an array of strings. Hashes: { 'name' => 'Peter', 'profession' => 'lion tamer', 'great love' => 'flannel' } Emptyness: plastic_cup = nil plastic_cup = false IF and UNLESS: if plastic_cup print "Plastic cup is on the up 'n' up!" end unless plastic_cup print "Plastic cup is on the down low." end <%= link_to 'host', item.from if item.from != "" -%> print "Yeah, plastic cup is up again!" if plastic_cup print "Hardly. It's down." unless plastic_cup print "We're using plastic 'cause we don't have glass." if plastic_cup unless glass_cup **WHEY** leading zeroes: "%05d" % 200 gives "00200" assigning dependent on... at_hotel = true email = if at_hotel else end --- looping through an array with a counter <% @items.each_with_index do |item, index| %> <%= index+1 %>. <%= item.name %> <% end %> --- ANY ? --- @sections.any? { |section| section.is_a?(String) } --- IS AN ARRAY ? --- @sections.is_a?(Array) --- forms --- <%= f.hidden_field :has_manufacture, :value => "No" %> --- count() ---- nav_items = navItems.size nav_items = navItems.length --- if / else -- if x == y beeroclock = 'now' elsif beeroclock = 'later' else beeroclock = 'over' end --- logging --- logger.warn "Failed login for '#{params[:email]}' from #{request.remote_ip} at #{Time.now.utc}" ---- sorting and eaching ---- <% @posts = Post.find(:all, :order => 'created_at DESC') %> <% @posts.each do |post| %> something <% end %> ---- more find examples ---- @items = @user.items.find(:all, :order => 'id DESC').paginate(:per_page => 100, :page => params[:page]) ---- STRING MANIPULATION ---- truncate(neighbourhood.description, :length => 260) pluralize(post.responses.size,'response') ---- to_formatted_s to_param to_query to_sentence to_xml http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/Array/Conversions.html#M000274 --- SCRIPT --- script/server - start server script/console - run console with all models... script/server -p 3001 -d - run server on port 3001 and detatch the console - ( use ps aux & kill to stop it ) script/server -p 3001 -e production - run server in production & on port 3001 --- DEBUGGING --- In A View <%= debug @object %> <%= debug 'string' %> <%= 'string'.inspect %> In A Controller logger.debug "The object is #{@object}" RAILS_DEFAULT_LOGGER.debug @object ---- RAKE ---- rake -T ( is the shit ) install, unpack and build gems in a rails project: rake gems:install rake gems:unpack rake gems:build rake routes -- gets you all the routs in your project point to -> production rake db:migrate RAILS_ENV=production clear ruby on rails cache rake tmp:cache:clear generate: script/generate migration MyNewMigration script/generate controller static ---- Mongrel ---- STOP MONGREL --daemon http://www.cyberciti.biz/faq/kill-process-in-linux-or-terminate-a-process-in-unix-or-linux-systems/ -- kill deamon -- ps aux (to get the id) kill id -- SNIPS -- <%= link_to "#{image_tag("go_back.png")}Go Back", user_path(@user) %> FINDERS ---------------------------- oldEntries = Posts.find( :all, :conditions => ['visible = ? and created_at <= ?', 'yes', (Time.now - daysToKeep.days)]) SCOPES ----------------------------- Inside the model: default_scope :order => 'name ASC' Entry.named_scope :lastweek, :conditions => ["created_at > ?", 7.day.ago] Entry.named_scope :isvisible, :conditions => {:visible => 'yes'} -- lambda makes sure the current date is uses and not a cached? one Entry.named_scope :lastweek, lambda { {:conditions => ["created_at > ?", 7.day.ago]} } -- Entries with AND ( headline AND blurb need to be 'bmx' ) Entry.scope_procedure :awesome, lambda { headline_like("bmx").blurb_like("bmx") } -- Entries with OR ( headline OR blurb need to be 'bmx' ) Entry.scope_procedure :awesome, lambda { headline_like_or_blurb_like("bmx") } Entry.named_scope :with_recent_orders, :conditions => ["comments.created_at >= ?", 1.week.ago] RENDER -------------------------- render :partial => "person", :locals => { :name => "david" } render :partial => "person", :object => @new_person render :partial => "person", :collection => @winners render :partial => "admin_person", :collection => @winners, :as => :person render :partial => "person", :collection => @winners, :spacer_template => "person_divider" render :partial => "shared/note", :collection => @new_notes render :partial => "broken", :status => 500 more: http://apidock.com/rails/ActionController/Base/render
You need to login to post a comment.
