1 / 28

Ruby on Rails

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!.

malana
Télécharger la présentation

Ruby on Rails

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Ruby on Rails Your first app

  2. Rails files

  3. Rails files

  4. Configuring your DB • SQLite: built-in support, lightweight, serverless

  5. Configuring your DB • MySQL: not built in, you need to configure properly the onfig/database.yml

  6. Create an empty DB

  7. 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:

  8. 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>

  9. Setting your application homepage • Open: config/routes.rb

  10. 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.

  11. Blog Example • Let’s create a blog app with Rails • Using Scaffold to create a Post resource that represents a single blog post:

  12. What Scaffold generator created

  13. What Scaffold generator created

  14. 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.

  15. Try a migration • More info at: http://guides.rubyonrails.org/migrations.html

  16. Add a link • app/views/home/index.html.erb

  17. 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.

  18. 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).

  19. Add Validation • Edit the model: • Validation overview: http://guides.rubyonrails.org/active_record_validations_callbacks.html#validations-overview

  20. Test validation

  21. Listing all posts • Open the file app/controllers/posts_controller.rb • Layouts and rendering: http://guides.rubyonrails.org/layouts_and_rendering.html

  22. Layout • Application specific layout in: app/views/layouts/application.html.erb • Edit! Make the body background a different color (other than white!)

  23. 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.

  24. 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

  25. Display an individual post • http://localhost:3000/posts/1 • Where is the code that shows post? • There are to parts: show action • Erb part

  26. Edit posts • Two parts again! Find them. • Actions: • Edit • Update

  27. Delete posts • What type of action? • How is the new page displayed?

  28. 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

More Related