ERB: Notes
Some notes…
Resources
“Convention over configuration”
- By default, controllers automatically render views with names that correspond to valid routes, eg:
- config/routes.rb: resources :books
- app/controllers/books_controller.rb: class BooksController < ApplicationController; end
- app/views/books/index.html.erb
- Rails also wraps the response in a layout: app/views/layouts/application.html.erb
- 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).
- Otherwise, rendering JSON is beautifully simple:
render :json => @product
More un-Pythonisms
- 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
- 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.
- Layout declarations cascade following the class hierarchy.
…
--
The real world is a special case