Overall Review
I actually learned a few new tips and tricks from this book. It was nice to read a book that's mainly focused on the view end of Rails. And I was surprised by the amount of things I learned since I didn't really know there was that much to talk about for views. However, the book did go in a bit of a tangent regarding Sass (I am biased though since I already knew the syntax before reading this book). Overall I would definitely recommend this book! Go check out their website and the rules they have on Rails view layer.
Chapter 1
- If you don't specify the folder in the partial file name, Rails will look for the file in the current controller’s view directory
- Use
yield :sidebar,content_for :sidebar do, andcontent_for?(:sidebar)to allow content in the view to hook into the layout. - There is a nifty solution for how to handle navigation in Rails. Put nav in a partial that uses
content_for. Then, use a helper method to render this partial with a local variable dictating what the current page is:
def currently_at(tab)
render partial: 'layouts/main_nav', locals: {current_tab: tab}
end-
flash[:notice],flash[:alert]and the correspondingalert, andnoticehelper methods. - ie_conditional_tag gem
Chapter 2
-
rawmethod (similar tohtml_safe),link_to,image_tag,number_to_currency,number_to_human_size - Use this to avoid putting law of demeter solution in model and avoid being tied to having @creation present (it's a default parameter):
def creation_client_name(creation = @creation)
creation.project.client.name
end- Use this to render collections so that the html for an item is abstracted away as well as the iteration (which should be Rails responsibility):
<%= render @creations %>- Move if/elsif/else clause into separate partials and a helper that renders the correct partial:
<%= controls_for_creation(creation) %>- extract long conditions to method_name? helpers. You can even
yieldin that method so that it can take a block of html - Use content_tag_for when attributes become complex (use
dom_id)
Chapter 3
- sprockets looks in these folders:
app/assetsfor application code,lib/assetsfor code used in several projects,vendor/assetsfor 3rd party code - directives:
require_treeto require a directory recursively and alphabetically,require_selffor the file itself -
rails runner "y Rails.configuration.assets.paths"to see where sprocket finds the files - Use require when you just want to shove content together and @import when you want to use the definitions present in the dependency in the current file
-
asset-pathandimage-pathSass functions - sprites (use compass)
-
@font-facecss declaration stylesheet_link_tag
Chapter 4
-
javascript_include_tag(include_js) - jquery_ujs
-
confirmparameter tolink_to -
remoteparameter to a form (render a js partial)
-
-
j/escape_javascriptmethod
Chapter 5
-
form_for(multipartoption),text_field,label,file_field,submit,form_tag - html fieldsets
- tabindex for tabbing order
- optgroup for grouping options. Pass an array of arrays to
select_tag - autofocus (html5)
- Rails automatically wraps div with field_with_errors
- form builder
error_messagesmethod (https://github.com/joelmoss/dynamic_form) fields_for-
ActionView::Helpers::FormBuilderYou can subclass this to add methods to the form builder object. Ex:
def field_item(attribute, text = nil, &block)
@template.content_tag :li do
@template.concat @template.label(attribute, text)
yield
end
endpass builder paramter to form_for
- input tag has new type options from html5
Chapter 6
- Make a presenter object and an associating helper method. Use a block so that the presenter object is yielded instead of assigning a variable for the presenter.
- We pass along the presenter instance using the
:objectoption, which will make sure it's assigned to a variable with the same name as the partial (in this case, status). This allows the partial to call methods on this object directly - We can make presenters render itself. Pass
selfto the presenter (self is the template object). Then add ato_sthat renders the partial corresponding to the presenter - test that the presenter returns correct values, helper returns presenter and that the
to_shas some correct markup, controller has some correct markup - Interesting syntax here:
@columns.each_with_object({}) do |(title, attribute), memo|Chapter 7
-
request.format = :mobileto create mobile version views (register mime type with Rails) - Allow users to override the agent query
link_to_unless- jQuery Mobile
Chapter 8
- ActionMailer
- Use ASCII characters for text version of emails and provide a link to view the email
- letter_opener gem
Chapter 9
- A/B testing with Vanity
- Deadweight gem
