290 likes | 537 Views
Ruby on Rails. Your first app. Rails files. Rails files. Configuring your DB. SQLite: built-in support, lightweight, serverless. Configuring your DB. MySQL: not built in, you need to configure properly the onfig / database.yml. Create an empty DB. Hello Rails!.
E N D
Ruby on Rails Your first app
Configuring your DB • SQLite: built-in support, lightweight, serverless
Configuring your DB • MySQL: not built in, you need to configure properly the onfig/database.yml
Hello Rails! • You need to create at minimum a controller and a view. • You can do that in a single command. Enter this command in your terminal:
erb • Rails will create several files for you • app/views/home/index.html.erb:is the template that will be used to display the results of the index action (method) in the home controller. • Edit this file in your text editor and edit it to contain a single line of code: <h1>Hello, Rails!</h1>
Setting your application homepage • Open: config/routes.rb
Scaffolding • Rails scaffolding is a quick way to generate some of the major pieces of an application. • If you want to create the models, views, and controllers for a new resource in a single operation, scaffolding is the tool for the job.
Blog Example • Let’s create a blog app with Rails • Using Scaffold to create a Post resource that represents a single blog post:
Database Migration • Migrations are Ruby classes that are designed to make it simple to create and modify database tables. • Use “rake”commands • Look in the db/migrate/20100207214725_create_posts.rb (yours will have a slightly different name) • When you run this migration it will create a posts table with two string columns and a text column. • It also creates two timestamp fields to allow Rails to track post creation and update times.
Try a migration • More info at: http://guides.rubyonrails.org/migrations.html
Add a link • app/views/home/index.html.erb
The Model • Check app/models/post.rb • ThePost class inherits fromActiveRecord::Base. • Active Record supplies a great deal of functionality to your Rails models for free, including • basic database CRUD (Create, Read, Update, Destroy) operations, • data validation, • sophisticated search support • ability to relate multiple models to one another.
The Model • attr_accessible: Remember Ruby OO? • It specifies a whitelist of attributes that are allowed to be updated in bulk (via update_attributes for instance).
Add Validation • Edit the model: • Validation overview: http://guides.rubyonrails.org/active_record_validations_callbacks.html#validations-overview
Listing all posts • Open the file app/controllers/posts_controller.rb • Layouts and rendering: http://guides.rubyonrails.org/layouts_and_rendering.html
Layout • Application specific layout in: app/views/layouts/application.html.erb • Edit! Make the body background a different color (other than white!)
Creating new posts • Instantiate new Post: where?? • Display a new Post: where?? • Open: views/posts/_form.html.erb • Where are all these classes defined? • What is the: @post? • What is the form_forblock: • used to create an HTML form. Within this block, you have access to methods to build various controls on the form • smart enough to work out if you are doing a New Post or an Edit Postaction, and will set the form action tags and submit button names appropriately in the HTMLoutput.
Creating new posts • What happens when you hit the create button? • POST or GET action? • If not successfully created: return to new! • If successfully created store to Rails flash hash
Display an individual post • http://localhost:3000/posts/1 • Where is the code that shows post? • There are to parts: show action • Erb part
Edit posts • Two parts again! Find them. • Actions: • Edit • Update
Delete posts • What type of action? • How is the new page displayed?
Resources • http://guides.rubyonrails.org/getting_started.html • http://ruby.railstutorial.org/chapters/a-demo-app#top • http://www.randomhacks.net/articles/2007/01/20/13-ways-of-looking-at-a-ruby-symbol