Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kirankarki/c5f436dd611420498897a048f31b9ee5 to your computer and use it in GitHub Desktop.
Save kirankarki/c5f436dd611420498897a048f31b9ee5 to your computer and use it in GitHub Desktop.
Learning note of "Agile web development with Rails 5" book
<< Agile web development with Rails 5 >>
<< Notes >>
< Gems documentation server >
1. run "gem server"
2. open "localhost:8808"
< VM for RoR core development >
$ git clone https://github.com/rails/rails-dev-box.git
$ cd rails-dev-box
$ vagrant up
< Description of a Project >
1. run "rails about"
< Enabling rails to be accessible in local network >
1. run "rails s -b 0.0.0.0
< Undo and reapply last migration >
1. run "rails db:migrate:redo"
< Caching in dev environment >
1. run 'rails dev:cache'
2. <% cache @products do %>
<% @products.each do |product| %>
<% cache product do %>
<div class="entry">
<%= image_tag(product.image_url) %>
<h3><%= product.title %></h3>
<%= sanitize(product.description) %>
<div class="price_line">
<span class="price"><%= number_to_currency(product.price) %></span>
</div>
</div>
<% end %>
<% end %>
<% end %>
< Rails db:migrate with status >
1. run 'rails db:migrate:status'
< Rails controller error logger >
1. Use the Rails logger to record the error. Every controller has a logger attribute. Here we use it to record a message at the error logging level.
2. Code 'logger.error "Attempt to access invalid cart #{params[:id]}"'
< Clear logs from Rails >
1. run 'rails log:clear LOGS=development'
< Summing column's values using Rails's sum method >
1. line_items.to_a.sum{ |item| item.total_price }
2. line_items.sum(&:total_price)
<< Rails's components >>
< Load anything from 'lib' folders >
- Add following code in 'config/application.rb'
> config.autoload_paths += %W(#{Rails.root}/lib)
< A Place for Script Wrappers >
- the bin directory is the place to put wrappers that call scripts that are launched from the command line and perform various maintenance tasks for your application.
< Rails Console for DB >
> rails dbconsole
< Removes autogenerated files created by generate >
> rails destroy ModelName
< Executes a method in your application outside the context of the Web >
> rails runner Order.test
<< 1. Active Record >>
< ActiveRecord's db attributes >
> run 'TableName.column_names' in 'rails console' to show all the columns of the given TableName
> run 'TableName.columns_hash['column_name']' to view detail info of the given column name
< Get raw value of attributes >
> run 'TableName.column_name_before_type_cast'
< Mapping SQL Types with Ruby >
SQL Type Ruby Class
int, integer Fixnum
float, double Float
decimal, numeric BigDecimal
char, varchar, string String
interval, date Date
datetime, time Time
clob, blob, text String
boolean See text
< Like Clause >
> User.where("name like ?", params[:name]+"%")
<< Concern >>
> If you find yourself repeating code, consider using concerns.
<< 2. Action Pack >>
> consists of three Ruby modules: ActionDispatch , ActionController , and ActionView
< ActionDispatch >
> routes requests to controllers
< ActionController >
> converts requests to responses
< ActionView >
> is used by ActionController to format those responses
< ActionDispatch >
< Routes >
> Adding language to routes
scope '(:locale)' do
resources :orders
end
> Making routes reusable
concern :reviewable do
resources :reviews
end
resources :products, concern: :reviewable
resources :users, concern: :reviewable
> Shallow Route Nesting
resources :products, shallow: true do
resources :reviews
end
< View >
> Overriding the deault location of "views" from "app/views" to smth
> ActionController.prepend_view_path dir_path
< Render Method >
> render(action: 'fake_action_name')
> render(template: 'controller/name')
> render(file: 'dir/template')
< Overriding default location of layout >
> class StoreController < ApplicationController
> layout "standard", except: [ :rss, :atom ]
> # ...
> end
< Spacer_template >
> :spacer_template parameter lets you specify a template that will be rendered between each of the elements in the collection
><%= render(partial: "animal", collection: %w{ ant bee cat dog elk }, spacer_template: "spacer") %>
<< Migrations >>
< Run a specific version migration >
> rails db:migrate VERSION=20160330000009
< Running multiple old migrations back >
> rails db:migrate:redo STEP=3
< Custom Messages and Benchmarking in migration file >
>def up
say_with_time "Updating prices..." do
Person.all.each do |p|
p.update_attribute :price, p.lookup_master_price
end
end
end
<< Rake for automating tasks >>
< List rake tasks of db:setup >
> rake --trace --dry-run db:setup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment