Date post: | 20-Jun-2015 |
Category: |
Technology |
Upload: | elpizoch |
View: | 308 times |
Download: | 2 times |
Ruby on RailsJune 17, 2011
Elpizo Choi
What is Ruby on Rails?
• MVC Framework for building web apps in Ruby• Uses Ruby classes to abstract out the various
components of an app• Convention over configuration
Main Components
• ActiveRecord (ORM – think hibernate)
• ActionController (business logic, routing)
• ActionView (template parser)
• Migrations, rake tasks
Creating a Rails app
Basic folder structure
apps foldercontrollers, models, views – where you’ll spend most your time
database.ymlConfigures database
GemfileConfigures external libraries used in app
… a little configuration
…a little configuration
Let’s make a model
ActiveRecord::Base
• When inherited, maps to table that’s plural of class name– Team -> teams
• Basic methods– Team.create()– Team.find()– Team.where().limit().sort().skip().first– Team.where().limit().sort().skip().all
…first we need to create table
…first we need to create table
…first we need to create table
Rake
• Ruby build tool• rake –T to see all available tasks• rake db:migrate will run all database migrations that haven’t been run• Stores migrations that have already been run
Create a new Team
Querying
More in-depth tutorial:
http://guides.rubyonrails.org/active_record_querying.html
The “web” part of web-app
Let’s get this running
What happened?
What happened?
What happened?
• config/routes.rb defines how urls are mapped to controllers– get “teams/index” is shorthand for:– match “teams/index” => “teams#index”, :via => :get
• More details: http://guides.rubyonrails.org/routing.html
• Read comments in config/routes.rb
Let’s make some changes
Let’s make some changes
Let’s make some changes
Controller
• Logic to be run every time the mapped route is hit
• All code in the corresponding method will happen as side effects
• Instanced variables set here are available to the template
View
• .erb is E-Ruby
• Add as extension to allow the default Rails template engine to parse Ruby code inside HTML
View• Basic HTML
• <% %> tag: Ruby code to be run, but not shown
• <%= %> tag: Ruby code to be run and shown
• ^^ important: because everything in Ruby returns something (in this case it’ll be @team object from the block)
Creating relationships
Creating relationships
Create the player
Add foreign key
Creating relationships
Creating relationships
Gotta reload console after changing models
Get players
But wait! There’s a shortcut!
has_many basically does the following:• looks in a table with same name as its argument (players) • for records whose foreign key column corresponds to the
current class (team_id)• and where the value is the current instance id (1)
Likewise…You can either do this…. …or this:
It’ll accomplish the same thing:
We can now change our view
We can now change our view
More associations
• ActiveRecord also has shortcuts for:– has_and_belongs_to (join tables)– Polymorphic associations (belongs_to different
objects)
• http://guides.rubyonrails.org/association_basics.html
Congratulations!You’ve made your first Rails app
Additional ResourcesSource code for example_app:https://github.com/fuJiin/example_app
See it live:http://cg-example-app.heroku.com/teams/ index
Documentation and light tutorials:http://guides.rubyonrails.org/
More in depth:http://ruby.railstutorial.org/ruby-on-rails-tutorial- book
Questions?