Skip to content

Commit e354e9b

Browse files
committed
Lesson 10 & 12
Signed-off-by: Yury Kaliada <fut.wrk@gmail.com>
1 parent c2ccf94 commit e354e9b

File tree

4 files changed

+621
-0
lines changed

4 files changed

+621
-0
lines changed

notes/10.md

Lines changed: 225 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,225 @@
1+
# Routing
2+
3+
```
4+
get '/patients/:id', to: 'patients#show'
5+
get 'profile', to: :show
6+
7+
get '/patients/:id', to: 'patients#show', as: 'patient'
8+
<%= link_to 'Patient Record', patient_path(@patient) %>
9+
```
10+
11+
## Links for resources
12+
```
13+
resources :photos
14+
15+
GET /photos index display a list of all photos
16+
GET /photos/new new return an HTML form for creating a new photo
17+
POST /photos create create a new photo
18+
GET /photos/:id show display a specific photo
19+
GET /photos/:id/edit edit return an HTML form for editing a photo
20+
PATCH/PUT /photos/:id update update a specific photo
21+
DELETE /photos/:id destroy delete a specific photo
22+
```
23+
24+
25+
## Links for resource
26+
```
27+
resource :geocoder
28+
29+
GET /geocoder/new new return an HTML form for creating the geocoder
30+
POST /geocoder create create the new geocoder
31+
GET /geocoder show display the one and only geocoder resource
32+
GET /geocoder/edit edit return an HTML form for editing the geocoder
33+
PATCH/PUT /geocoder update update the one and only geocoder resource
34+
DELETE /geocoder destroy delete the geocoder resource
35+
```
36+
37+
## Controller Namespaces and Routing
38+
```
39+
namespace :admin do
40+
resources :posts, :comments
41+
end
42+
43+
GET /admin/posts index admin_posts_path
44+
GET /admin/posts/new new new_admin_post_path
45+
POST /admin/posts create admin_posts_path
46+
GET /admin/posts/:id show admin_post_path(:id)
47+
GET /admin/posts/:id/edit edit edit_admin_post_path(:id)
48+
PATCH/PUT /admin/posts/:id update admin_post_path(:id)
49+
DELETE /admin/posts/:id destroy admin_post_path(:id)
50+
```
51+
52+
```
53+
scope '/admin' do
54+
resources :posts, :comments
55+
end
56+
57+
GET /admin/posts index posts_path
58+
GET /admin/posts/new new new_post_path
59+
POST /admin/posts create posts_path
60+
GET /admin/posts/:id show post_path(:id)
61+
GET /admin/posts/:id/edit edit edit_post_path(:id)
62+
PATCH/PUT /admin/posts/:id update post_path(:id)
63+
DELETE /admin/posts/:id destroy post_path(:id)
64+
```
65+
66+
## Nested Resources
67+
```
68+
class Magazine < ActiveRecord::Base
69+
has_many :ads
70+
end
71+
72+
class Ad < ActiveRecord::Base
73+
belongs_to :magazine
74+
end
75+
76+
77+
resources :magazines do
78+
resources :ads
79+
end
80+
81+
82+
GET /magazines/:magazine_id/ads index display a list of all ads for a specific magazine
83+
GET /magazines/:magazine_id/ads/new new return an HTML form for creating a new ad belonging to a specific magazine
84+
POST /magazines/:magazine_id/ads create create a new ad belonging to a specific magazine
85+
GET /magazines/:magazine_id/ads/:id show display a specific ad belonging to a specific magazine
86+
GET /magazines/:magazine_id/ads/:id/edit edit return an HTML form for editing an ad belonging to a specific magazine
87+
PATCH/PUT /magazines/:magazine_id/ads/:id update update a specific ad belonging to a specific magazine
88+
DELETE /magazines/:magazine_id/ads/:id destroy delete a specific ad belonging to a specific magazine
89+
90+
91+
resources :publishers do
92+
resources :magazines do
93+
resources :photos
94+
end
95+
end
96+
```
97+
98+
## Shallow nesting
99+
```
100+
resources :posts do
101+
resources :comments, only: [:index, :new, :create]
102+
end
103+
resources :comments, only: [:show, :edit, :update, :destroy]
104+
105+
resources :posts do
106+
resources :comments, shallow: true
107+
end
108+
109+
resources :posts, shallow: true do
110+
resources :comments
111+
resources :quotes
112+
resources :drafts
113+
end
114+
115+
shallow do
116+
resources :posts do
117+
resources :comments
118+
resources :quotes
119+
resources :drafts
120+
end
121+
end
122+
123+
scope shallow_path: "sekret" do
124+
resources :posts do
125+
resources :comments, shallow: true
126+
end
127+
end
128+
```
129+
130+
## Routing concerns
131+
```
132+
concern :commentable do
133+
resources :comments
134+
end
135+
136+
concern :image_attachable do
137+
resources :images, only: :index
138+
end
139+
140+
141+
142+
resources :messages, concerns: :commentable
143+
144+
resources :posts, concerns: [:commentable, :image_attachable]
145+
146+
147+
148+
resources :messages do
149+
resources :comments
150+
end
151+
152+
resources :posts do
153+
resources :comments
154+
resources :images, only: :index
155+
end
156+
157+
158+
namespace :posts do
159+
concerns :commentable
160+
end
161+
```
162+
163+
164+
## Links
165+
```
166+
<%= link_to 'Ad details', magazine_ad_path(@magazine, @ad) %>
167+
<%= link_to 'Ad details', url_for([@magazine, @ad]) %>
168+
<%= link_to 'Ad details', [@magazine, @ad] %>
169+
<%= link_to 'Magazine details', @magazine %>
170+
<%= link_to 'Edit Ad', [:edit, @magazine, @ad] %>
171+
```
172+
173+
## Adding More RESTful Actions
174+
```
175+
resources :photos do
176+
member do
177+
get 'preview'
178+
end
179+
end
180+
181+
resources :photos do
182+
get 'preview', on: :member
183+
end
184+
185+
resources :photos do
186+
collection do
187+
get 'search'
188+
end
189+
end
190+
191+
resources :photos do
192+
get 'search', on: :collection
193+
end
194+
```
195+
196+
197+
```
198+
get ':controller(/:action(/:id))'
199+
200+
get ':controller(/:action(/:id))', controller: /admin\/[^\/]+/
201+
202+
get ':controller/:action/:id/with_user/:user_id'
203+
204+
```
205+
206+
## HTTP Verb Constraints
207+
```
208+
match 'photos', to: 'photos#show', via: [:get, :post]
209+
210+
match 'photos', to: 'photos#show', via: :all
211+
```
212+
213+
## Segment Constraints
214+
```
215+
get 'photos/:id', to: 'photos#show', constraints: { id: /[A-Z]\d{5}/ }
216+
217+
get '/stories', to: redirect('/posts')
218+
get '/stories/:name', to: redirect('/posts/%{name}')
219+
get '/stories/:name', to: redirect {|params, req| "/posts/#{params[:name].pluralize}" }
220+
221+
222+
root to: 'pages#main'
223+
root 'pages#main' # shortcut for the above
224+
```
225+

notes/12_controllers.md

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
# Controller Naming Convention
2+
# Methods and Actions
3+
# Parameters
4+
# Hash and Array Parameters
5+
GET /clients?ids[]=1&ids[]=2&ids[]=3
6+
# JSON parameters
7+
# Routing Parameters
8+
# default_url_options
9+
# Strong Parameters
10+
# Permitted Scalar Values (permit!)
11+
# Nested Parameters
12+
```
13+
params.permit(:name, { emails: [] },
14+
              friends: [ :name,
15+
                         { family: [ :name ], hobbies: [] }])
16+
```
17+
# Fetch Params
18+
```
19+
params.fetch(:blog, {}).permit(:title, :author)
20+
```
21+
# Session
22+
```
23+
ActionDispatch::Session::CookieStore - Stores everything on the client.
24+
ActionDispatch::Session::CacheStore - Stores the data in the Rails cache.
25+
ActionDispatch::Session::ActiveRecordStore - Stores the data in a database using Active Record. (require activerecord-session_store gem).
26+
ActionDispatch::Session::MemCacheStore - Stores the data in a memcached cluster (this is a legacy implementation; consider using CacheStore instead).
27+
```
28+
# Accessing the Session
29+
Sessions are lazily loaded
30+
reset_session
31+
#The Flash
32+
```
33+
flash[:notice] = "You have successfully logged out."
34+
    
35+
redirect_to root_url, notice: "You have successfully logged out."
36+
redirect_to root_url, alert: "You're stuck here!"
37+
redirect_to root_url, flash: { referral_code: 1234 }
38+
39+
<% flash.each do |name, msg| -%>
40+
<%= content_tag :div, msg, class: name %>
41+
<% end -%>
42+
43+
flash.now[:error] = "Could not save client"
44+
```
45+
46+
# Cookies
47+
```
48+
        cookies[:commenter_name] = @comment.author
49+
```
50+
51+
# Rendering xml and json data
52+
```
53+
respond_to do |format|
54+
format.html # index.html.erb
55+
format.xml { render xml: @users}
56+
format.json { render json: @users}
57+
end
58+
```
59+
60+
# Filter
61+
```
62+
class ApplicationController < ActionController::Base
63+
before_action :require_login
64+
65+
private
66+
67+
def require_login
68+
unless logged_in?
69+
flash[:error] = "You must be logged in to access this section"
70+
redirect_to new_login_url # halts request cycle
71+
end
72+
end
73+
74+
  skip_before_action :require_login, only: [:new, :create]
75+
```
76+
77+
# After Filters and Around Filters
78+
```
79+
class ChangesController < ApplicationController
80+
around_action :wrap_in_transaction, only: :show
81+
82+
private
83+
84+
def wrap_in_transaction
85+
ActiveRecord::Base.transaction do
86+
begin
87+
yield
88+
ensure
89+
raise ActiveRecord::Rollback
90+
end
91+
end
92+
end
93+
end
94+
95+
class ApplicationController < ActionController::Base
96+
  before_action do |controller|
97+
    redirect_to new_login_url unless controller.send(:logged_in?)
98+
  end
99+
end
100+
```
101+
102+
# Request Forgery Protection
103+
# The Request and Response Objects
104+
# Headers
105+
```
106+
response.headers["Content-Type"] = "application/pdf"
107+
```
108+
# HTTP Basic Authentication
109+
```
110+
class AdminsController < ApplicationController
111+
http_basic_authenticate_with name: "humbaba", password: "5baa61e4"
112+
end
113+
```
114+
# HTTP Digest Authentication
115+
```
116+
class AdminsController < ApplicationController
117+
USERS = { "lifo" => "world" }
118+
119+
before_action :authenticate
120+
121+
private
122+
123+
def authenticate
124+
authenticate_or_request_with_http_digest do |username|
125+
USERS[username]
126+
end
127+
end
128+
end
129+
```
130+
131+
# Streaming and File Downloads
132+
```
133+
send_data generate_pdf(client),
134+
              filename: "#{client.name}.pdf",
135+
              type: "application/pdf"
136+
```
137+
138+
# Send file
139+
```
140+
send_file("#{Rails.root}/files/clients/#{client.id}.pdf",
141+
              filename: "#{client.name}.pdf",
142+
              type: "application/pdf")
143+
```
144+
145+
# Parameters Filtering
146+
config.filter_parameters << :password
147+
# Redirects Filtering
148+
config.filter_redirect << 's3.amazonaws.com' or regexp
149+
# The Default 500 and 404 Templates
150+
404.html and 500.html
151+
# rescue_from
152+
```
153+
class ApplicationController < ActionController::Base
154+
rescue_from ActiveRecord::RecordNotFound, with: :record_not_found
155+
156+
private
157+
158+
def record_not_found
159+
render text: "404 Not Found", status: 404
160+
end
161+
end
162+
```
163+
164+
# Force HTTPS protocol
165+
```
166+
class DinnerController
167+
force_ssl only: :cheeseburger
168+
# or
169+
force_ssl except: :cheeseburger
170+
end
171+
```

notes/12_controllers.pdf

47.4 KB
Binary file not shown.

0 commit comments

Comments
 (0)