Rails review


Published on

ATLRUG Emerald City Programming Group, April 23, 2011.

Published in: Technology, Education
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Rails review

  1. 1. Rails Review Alan Hecht http://alanhecht.me
  2. 2. Building a Rails Application <ul><ul><ul><li>How would you build emerald_blogger </li></ul></ul></ul><ul><ul><ul><li>Rails 3 application architecture </li></ul></ul></ul>
  3. 3. Application Setup <ul><ul><ul><li>rails new emerald_blogger </li></ul></ul></ul><ul><ul><ul><li>Edit Gemfile </li></ul></ul></ul><ul><ul><ul><ul><li>bundle install </li></ul></ul></ul></ul><ul><ul><ul><li>Add project to Git </li></ul></ul></ul><ul><ul><ul><li>Set up project in GitHub </li></ul></ul></ul><ul><ul><ul><li>Link project to GitHub </li></ul></ul></ul>
  4. 4. App. Configuration Client Files <ul><ul><ul><li>Located in the ‘public’ directory </li></ul></ul></ul><ul><ul><ul><li>HTML, image, JavaScript, and stylesheet files </li></ul></ul></ul><ul><ul><ul><li>Default page public/index.html </li></ul></ul></ul>
  5. 5. App. Configuration - Environments <ul><ul><ul><li>Three environments: development, test, & production </li></ul></ul></ul><ul><ul><ul><li>Can add additional environments (i.e. acceptance) </li></ul></ul></ul>
  6. 6. App. Configuration - Database <ul><ul><ul><li>config/database.yml has database configuration </li></ul></ul></ul><ul><ul><ul><li>Databases located in db/ </li></ul></ul></ul><ul><ul><ul><li>db/schema.rb contains the database schema after migrations are applied </li></ul></ul></ul><ul><ul><ul><li>db/seeds.rb contains database seed data which is run once </li></ul></ul></ul>
  7. 7. App. Configuration - Defaults <ul><ul><ul><li>Located in config/application.rb </li></ul></ul></ul><ul><ul><ul><li>New to Rails 3 </li></ul></ul></ul>
  8. 8. Application Code <ul><ul><ul><li>Located in app/ </li></ul></ul></ul><ul><ul><ul><ul><li>app/models </li></ul></ul></ul></ul><ul><ul><ul><ul><li>app/views </li></ul></ul></ul></ul><ul><ul><ul><ul><li>app/controllers </li></ul></ul></ul></ul><ul><ul><ul><ul><li>app/helpers </li></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>Helper code for models, views, and controllers </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>Primarily seen it used for view helpers, with some controller helpers </li></ul></ul></ul></ul></ul>
  9. 9. Post Setup - Model <ul><ul><ul><li>rails generate model Post title:string, content:text, post_date:date </li></ul></ul></ul><ul><ul><ul><li>Model, migration, and test were created </li></ul></ul></ul>
  10. 10. Post Setup - Validation <ul><ul><ul><li>In Rails 3, validations are “validates <field(s)>, <condition>” </li></ul></ul></ul><ul><ul><ul><li>validates :title, :content, :presence => true </li></ul></ul></ul>
  11. 11. Post Setup - Migration <ul><ul><ul><li>rails generate migration AddLikesToPosts num_likes:integer </li></ul></ul></ul><ul><ul><ul><li>run with “rake db:migrate” </li></ul></ul></ul><ul><ul><ul><li>Can add an index to a table as a migration </li></ul></ul></ul>
  12. 12. Post Setup - Routing <ul><ul><ul><li>config/routes.rb </li></ul></ul></ul><ul><ul><ul><li>A “resource route” maps HTTP verbs to CRUD actions </li></ul></ul></ul><ul><ul><ul><li>Add a “resources :posts” line to file </li></ul></ul></ul><ul><ul><ul><li>rake routes – lists the routes and their actions </li></ul></ul></ul><ul><ul><ul><li>Must restart the application after changing the file </li></ul></ul></ul>
  13. 13. Post Setup - Controller <ul><ul><ul><li>Ties the view and the model together </li></ul></ul></ul><ul><ul><ul><li>Sets up the model(s) for use by the view </li></ul></ul></ul><ul><ul><ul><li>The view with the same action will be rendered unless a different view is specified </li></ul></ul></ul><ul><ul><ul><ul><li>i.e. render :edit </li></ul></ul></ul></ul><ul><ul><ul><ul><li>Controller code for different view is not executed </li></ul></ul></ul></ul><ul><ul><ul><li>Can specify what view layout to use along with alternatives to HTML like XML or JavaScript </li></ul></ul></ul>
  14. 14. Views - Layouts <ul><ul><ul><li>Page template </li></ul></ul></ul><ul><ul><ul><li>View code for an action executed by the “yield” statement </li></ul></ul></ul><ul><ul><ul><ul><li>- Implemented using a Ruby block </li></ul></ul></ul></ul>
  15. 15. Post Setup - View <ul><ul><ul><li>app/views/posts directory </li></ul></ul></ul>
  16. 16. Views - Partials <ul><ul><ul><li>app/views/posts/_form.html.erb </li></ul></ul></ul><ul><ul><ul><ul><li>Underscore in file name signifies that it’s a partial </li></ul></ul></ul></ul><ul><ul><ul><li>Represents a portion of the view on a specific page that serves a specific purpose or is reused. </li></ul></ul></ul><ul><ul><ul><li>Can pass parameters into partials </li></ul></ul></ul><ul><ul><ul><li>Most of the time, the partial is loaded in the view code </li></ul></ul></ul><ul><ul><ul><ul><li>Can render a partial directly from the controller </li></ul></ul></ul></ul>
  17. 17. App. Configuration Default Home Page <ul><ul><ul><li>Remove ‘public/index.html’ </li></ul></ul></ul><ul><ul><ul><li>Add the line ‘root :to => “<controller>#<action” in config/routes.rb </li></ul></ul></ul><ul><ul><ul><li>Restart the application </li></ul></ul></ul>
  18. 18. Comments Setup Active Record Associations <ul><ul><ul><li>Must be associated in the model and the database </li></ul></ul></ul><ul><ul><ul><li>For a “one-to-many” relationship: </li></ul></ul></ul><ul><ul><ul><ul><li>Add the foreign key to the parent in the child table </li></ul></ul></ul></ul><ul><ul><ul><ul><li>Add a “ has_many <children> ” line to the parent model </li></ul></ul></ul></ul>
  19. 19. Deploy to Heroku <ul><ul><ul><li>Edit Gemfile </li></ul></ul></ul><ul><ul><ul><ul><li>Add “gem heroku” line </li></ul></ul></ul></ul><ul><ul><ul><li>bundle install </li></ul></ul></ul><ul><ul><ul><li>heroku create </li></ul></ul></ul><ul><ul><ul><li>git push –u heroku master </li></ul></ul></ul><ul><ul><ul><li>heroku open </li></ul></ul></ul>