Rails 3: Difference between revisions

From Wiki
Jump to navigation Jump to search
Line 30: Line 30:
match 'products/:id' => 'products#show'
match 'products/:id' => 'products#show'
</pre>
</pre>
The url <code>http://localhost:3000/products/8</code> will be mapped to the <code>show</code> action of the <code>products</code> controller with <code>params[:id]</code> set to 8
The url <nowiki><code>http://localhost:3000/products/8</code></nowiki> will be mapped to the <code>show</code> action of the <code>products</code> controller with <code>params[:id]</code> set to 8


== Logging ==
== Logging ==

Revision as of 19:34, 16 February 2011

Application Setup

  • create project with
rails new my_project
  • add gem dependencies to Gemfile at the root level
  • update config/application.rb to load needed dependencies and update defaults
  • double-check /config/initializers and /config/environments

Files in lib/ are not automatically loaded, so you need to require them.

In config/environments/development.rb, set

config.action_mailer.perform_deliveries = false

No delivery attempt is performed, but you can still see the mail in the log file to check it looks good


Other rails commands

rails console
rails server
rails runner


Routing

Configured in config/routes.rb

match 'products/:id' => 'products#show'

The url <code>http://localhost:3000/products/8</code> will be mapped to the show action of the products controller with params[:id] set to 8

Logging

Use these in models, views, controllers to send timestamped messages to the log.

logger.debug "debug message"
logger.info "info message"
logger.warn "something bad"
logger.error "something broke"
logger.fatal "application dead"