|
|
(30 intermediate revisions by the same user not shown) |
Line 1: |
Line 1: |
| == Application Setup ==
| |
| * create project with
| |
| <pre>
| |
| rails new my_project
| |
| rails -d mysql new my_project # if you don't want the default sqlite3
| |
| </pre>
| |
| * add gem dependencies to <code>Gemfile</code> at the root level
| |
| * update <code>config/application.rb</code> to load needed dependencies and update defaults
| |
| * double-check <code>/config/initializers</code> and <code>/config/environments</code>
| |
| * edit <code>config/database.yml</code> to connect to your database
| |
|
| |
|
| === Notes ===
| |
| Files in <code>lib/</code> are not automatically loaded, so you need to <code>require</code> them.
| |
|
| |
| In <code>config/environments/development.rb</code>, set
| |
| <pre>
| |
| config.action_mailer.perform_deliveries = false
| |
| </pre>
| |
| No delivery attempt is performed, but you can still see the mail in the log file to check it looks good
| |
|
| |
| == Database Setup ==
| |
| * create database with
| |
| <pre>
| |
| rake db:create
| |
| </pre>
| |
|
| |
| == Other rails commands ==
| |
| <pre>
| |
| rails console
| |
| rails dbconsole
| |
| rails server
| |
| rails runner
| |
| </pre>
| |
|
| |
| == Routing ==
| |
| Configured in <code>config/routes.rb</code>
| |
| <pre>
| |
| match 'products/:id' => 'products#show'
| |
| </pre>
| |
| The url <code><nowiki>http://localhost:3000/products/8</nowiki></code> will be mapped to the <code>show</code> action of the <code>products</code> controller with <code>params[:id]</code> set to 8
| |
|
| |
| To create a link to this route (old way):
| |
| <pre>
| |
| link_to "Products", :controller => "products", :action => "show", :id => 1
| |
| </pre>
| |
|
| |
| To restrict the HTTP method, use get or post instead of match:
| |
| <pre>
| |
| get 'products/:id' => 'products#show'
| |
| </pre>
| |
|
| |
| To redirect:
| |
| <pre>
| |
| match "/foo", :to => redirect("/bar")
| |
| </pre>
| |
|
| |
| == Logging ==
| |
| Use these in models, views, controllers to send timestamped messages to the log.
| |
| <pre>
| |
| logger.debug "debug message"
| |
| logger.info "info message"
| |
| logger.warn "something bad"
| |
| logger.error "something broke"
| |
| logger.fatal "application dead"
| |
| </pre>
| |