Action View - The Rails 5 Way, Fourth Edition
Action View - The Rails 5 Way, Fourth Edition
safaribooksonline.com
The very powerful and the very stupid have one thing in
common. Instead of altering their views to fit the facts,
they alter the facts to fit their views…which can be very
uncomfortable if you happen to be one of the facts that
needs altering.
—Doctor Who
1 of 31 11/12/17, 3:55 PM
10. Action View - The Rails 5 Way, Fourth Edition about:reader?url=https://www.safaribooksonline.com/library/vie...
http://haml-lang.com/
2 of 31 11/12/17, 3:55 PM
10. Action View - The Rails 5 Way, Fourth Edition about:reader?url=https://www.safaribooksonline.com/library/vie...
• locale (optional)
• content type
• templating engine(s)
Layouts
3 of 31 11/12/17, 3:55 PM
10. Action View - The Rails 5 Way, Fourth Edition about:reader?url=https://www.safaribooksonline.com/library/vie...
!!! 5
%html
%head
%meta{ charset: 'utf-8' }
%title TR4W Time and Expenses Sample
4 of 31 11/12/17, 3:55 PM
10. Action View - The Rails 5 Way, Fourth Edition about:reader?url=https://www.safaribooksonline.com/library/vie...
Application
= csrf_meta_tag
= stylesheet_link_tag 'application',
media: 'all'
= javascript_include_tag
'application'
%body
=yield
Yielding Content
%body
= yield
You can add extra places in your layout where you want
to be able to yield content by including additional yield
invocations—just make sure to pass a unique identifier
as the argument. A good example is a layout that has
left and right sidebar content (simplified, of course):
5 of 31 11/12/17, 3:55 PM
10. Action View - The Rails 5 Way, Fourth Edition about:reader?url=https://www.safaribooksonline.com/library/vie...
%body
.left.sidebar
= yield :left
.content
= yield
.right.sidebar
= yield :right
- content_for :left do
%h2 Navigation
%ul
%li
- content_for :right do
%h2 Help
%p Lorem ipsum dolor sit amet,
consectetur adipisicing elit...
6 of 31 11/12/17, 3:55 PM
10. Action View - The Rails 5 Way, Fourth Edition about:reader?url=https://www.safaribooksonline.com/library/vie...
Listing 10.2.
!!! 5
%html
%head
%meta{ charset: 'utf-8' }
%title TR4W Time and Expenses Sample
Application
= csrf_meta_tag
= stylesheet_link_tag 'application',
media: 'all'
= javascript_include_tag
'application'
= yield :head
%body
= yield
Kevin says…
Conditional Output
7 of 31 11/12/17, 3:55 PM
10. Action View - The Rails 5 Way, Fourth Edition about:reader?url=https://www.safaribooksonline.com/library/vie...
- if show_subtitle?
%h2 = article.subtitle
8 of 31 11/12/17, 3:55 PM
10. Action View - The Rails 5 Way, Fourth Edition about:reader?url=https://www.safaribooksonline.com/library/vie...
show_subtitle?
Decent Exposure
https://github.com/hashrocket/decent_exposure
9 of 31 11/12/17, 3:55 PM
10. Action View - The Rails 5 Way, Fourth Edition about:reader?url=https://www.safaribooksonline.com/library/vie...
expose :timesheet
Timesheet.find(params[:timesheet_id] ||
params[:id])
expose :client
expose :timesheet, scope: -> {
client.timesheets }
10 of 31 11/12/17, 3:55 PM
10. Action View - The Rails 5 Way, Fourth Edition about:reader?url=https://www.safaribooksonline.com/library/vie...
expose :client
expose :timesheet, scope: -> {
client.timesheets }
expose :timesheet_approval_presenter do
TimesheetApprovalPresenter.new(timesheet,
current_user)
end
or a symbol
expose :client
expose :timesheet, scope: -> {
client.timesheets }
expose :timesheet_approval_presenter,
:setup_presenter
...
private
def setup_presenter
...
TimesheetApprovalPresenter.
new(timesheet, current_user)
end
11 of 31 11/12/17, 3:55 PM
10. Action View - The Rails 5 Way, Fourth Edition about:reader?url=https://www.safaribooksonline.com/library/vie...
assigns
base_path
controller
12 of 31 11/12/17, 3:55 PM
10. Action View - The Rails 5 Way, Fourth Edition about:reader?url=https://www.safaribooksonline.com/library/vie...
%body { class:
"#{controller.controller_name}
#{controller.action_name}" }
Note
13 of 31 11/12/17, 3:55 PM
10. Action View - The Rails 5 Way, Fourth Edition about:reader?url=https://www.safaribooksonline.com/library/vie...
body {
.timesheets .header {
background: image_url(timesheet-
bg.png) no-repeat left top ;
}
.expense_reports .header {
background: image_url(expense-
reports-bg.png) no-repeat left top;
}
}
cookies
flash
def create
if user.try(:authorize, params[:user]
[:password])
14 of 31 11/12/17, 3:55 PM
10. Action View - The Rails 5 Way, Fourth Edition about:reader?url=https://www.safaribooksonline.com/library/vie...
Note
def create
if user.try(:authorize, params[:user]
[:password])
redirect_to home_url, notice:
"Welcome, #{user.first_name}!"
else
redirect_to home_url, alert: "Bad
login"
end
end
15 of 31 11/12/17, 3:55 PM
10. Action View - The Rails 5 Way, Fourth Edition about:reader?url=https://www.safaribooksonline.com/library/vie...
def create
if user.try(:authorize, params[:user]
[:password])
redirect_to home_url, notice:
"Welcome, #{user.first_name}!"
else
redirect_to action: "new ", alert:
"Login invalid."
end
end
%html
...
%body
- if flash.notice
.notice= flash.notice
16 of 31 11/12/17, 3:55 PM
10. Action View - The Rails 5 Way, Fourth Edition about:reader?url=https://www.safaribooksonline.com/library/vie...
- if flash.alert
.notice.alert= flash.alert
= yield
The CSS for .notice defines most of the style for the
element, and .alert overrides just the aspects that
are different for alerts.
flash.now
17 of 31 11/12/17, 3:55 PM
10. Action View - The Rails 5 Way, Fourth Edition about:reader?url=https://www.safaribooksonline.com/library/vie...
render :new
end
end
end
logger
params
session
18 of 31 11/12/17, 3:55 PM
10. Action View - The Rails 5 Way, Fourth Edition about:reader?url=https://www.safaribooksonline.com/library/vie...
Partials
%h1 Details
= render 'details'
19 of 31 11/12/17, 3:55 PM
10. Action View - The Rails 5 Way, Fourth Edition about:reader?url=https://www.safaribooksonline.com/library/vie...
20 of 31 11/12/17, 3:55 PM
10. Action View - The Rails 5 Way, Fourth Edition about:reader?url=https://www.safaribooksonline.com/library/vie...
/users/_opt_in.html.haml
%fieldset#opt_in
%legend Spam Opt In
%p
= check_box :user,
:send_event_updates
Send me updates about events!
%br
= check_box :user, :send_site_updates
Notify me about new services
Reuse of Partials
21 of 31 11/12/17, 3:55 PM
10. Action View - The Rails 5 Way, Fourth Edition about:reader?url=https://www.safaribooksonline.com/library/vie...
Shared Partials
22 of 31 11/12/17, 3:55 PM
10. Action View - The Rails 5 Way, Fourth Edition about:reader?url=https://www.safaribooksonline.com/library/vie...
...
.terms
= render 'terms'
.captcha
= render 'shared/captcha'
%p = submit_tag 'Register'
= form_tag do
%fieldset
%label
Username:
= text_field_tag :username,
params[:username]
23 of 31 11/12/17, 3:55 PM
10. Action View - The Rails 5 Way, Fourth Edition about:reader?url=https://www.safaribooksonline.com/library/vie...
%br
%label
Password:
= password_field_tag :password,
params[:password]
%br
= submit_tag "Login"
24 of 31 11/12/17, 3:55 PM
10. Action View - The Rails 5 Way, Fourth Edition about:reader?url=https://www.safaribooksonline.com/library/vie...
25 of 31 11/12/17, 3:55 PM
10. Action View - The Rails 5 Way, Fourth Edition about:reader?url=https://www.safaribooksonline.com/library/vie...
%fieldset .address
%legend Address
%p
%label Street
%br
= form.text_area :street, rows: 2,
cols: 40
%p
%label City
%br
= form.text_field :city
%p
%label State
%br
= form.text_field :state, size: 2
%p
%label Zip
%br
26 of 31 11/12/17, 3:55 PM
10. Action View - The Rails 5 Way, Fourth Edition about:reader?url=https://www.safaribooksonline.com/library/vie...
- if local_assigns.has_key? :special
= special
Rendering an Object
= render entry
27 of 31 11/12/17, 3:55 PM
10. Action View - The Rails 5 Way, Fourth Edition about:reader?url=https://www.safaribooksonline.com/library/vie...
Rendering Collections
= render entries
28 of 31 11/12/17, 3:55 PM
10. Action View - The Rails 5 Way, Fourth Edition about:reader?url=https://www.safaribooksonline.com/library/vie...
= div_for(entry) do
= entry.description
#{distance_of_time_in_words_to_now
entry.created_at} ago
Kevin says…
29 of 31 11/12/17, 3:55 PM
10. Action View - The Rails 5 Way, Fourth Edition about:reader?url=https://www.safaribooksonline.com/library/vie...
= div_for(entry) do
"#{entry_counter}:#{entry.description}
#{distance_of_time_in_words_to_now
entry.created_at} ago"
If you wanted to use the same partial that you use with
a collection, except with a single entry object, you’d
have to pass it that single instance via the locals hash
described in the preceding section, like this:
Logging
30 of 31 11/12/17, 3:55 PM
10. Action View - The Rails 5 Way, Fourth Edition about:reader?url=https://www.safaribooksonline.com/library/vie...
Conclusion
31 of 31 11/12/17, 3:55 PM