ERB: Notes

Some notes…

Resources

  1. Tutorial

“Convention over configuration”

  1. By default, controllers automatically render views with names that correspond to valid routes, eg:
    1. config/routes.rb: resources :books
    2. app/controllers/books_controller.rb: class BooksController < ApplicationController; end
    3. app/views/books/index.html.erb
  2. Rails also wraps the response in a layout: app/views/layouts/application.html.erb
  3. For my current project, I want to use views without models (or models that aren’t data backed?) to wrap external services/resources (no JSON unfortunately, just HTML, pro’ly).
  4. Otherwise, rendering JSON is beautifully simple:
    render :json => @product

More un-Pythonisms

  1. Y’know, Python’s “zen” decrees:

    There should be one — and preferably only one — obvious way to do it.

    But in Rails all the following are equivalent!

    render :edit
    render :action => :edit
    render 'edit'
    render 'edit.html.erb'
    render :action => 'edit'
    render :action => 'edit.html.erb'
    render 'books/edit'
    render 'books/edit.html.erb'
    render :template => 'books/edit'
    render :template => 'books/edit.html.erb'
    render '/path/to/rails/app/views/books/edit'
    render '/path/to/rails/app/views/books/edit.html.erb'
    render :file => '/path/to/rails/app/views/books/edit'
    render :file => '/path/to/rails/app/views/books/edit.html.erb'

    I’m just saying.

Layouts

  1. By default, Rails looks for a layout file in app/views/layouts/ with the same base name as the controller, or else application.html.erb, or a layout specified in the ApplicationController class, etc.
  2. Layout declarations cascade following the class hierarchy.



Comments are closed.