Presentation is loading. Please wait.

Presentation is loading. Please wait.

Ruby on Rails Your first app. Rails files app/ Contains the controllers, models, views and assets for your application. You’ll focus on this folder for.

Similar presentations


Presentation on theme: "Ruby on Rails Your first app. Rails files app/ Contains the controllers, models, views and assets for your application. You’ll focus on this folder for."— Presentation transcript:

1 Ruby on Rails Your first app

2 Rails files app/ Contains the controllers, models, views and assets for your application. You’ll focus on this folder for the remainder of this guide. config/ Configure your application’s runtime rules, routes, database, and more. This is covered in more detail in Configuring Rails ApplicationsConfiguring Rails Applications db/ Contains your current database schema, as well as the database migrations. doc/ In-depth documentation for your application. lib/ Extended modules for your application.

3 Rails files log/Application log files. public/ The only folder seen to the world as- is. Contains the static files and compiled assets. script/ Contains the rails script that starts your app and can contain other scripts you use to deploy or run your application. test/ Unit tests, fixtures, and other test apparatus. These are covered in Testing Rails ApplicationsTesting Rails Applications config.ru Rack configuration for Rack based servers used to start the application. Gemfile Gemfile.lock These files allow you to specify what gem dependencies are needed for your Rails application.

4 Configuring your DB SQLite: built-in support, lightweight, serverless development: adapter: sqlite3 database: db/development.sqlite3 pool: 5 timeout: 5000

5 Configuring your DB MySQL: not built in, you need to configure properly the onfig/database.yml development: adapter: postgresql encoding: unicode database: blog_development pool: 5 username: blog password

6 Create an empty DB $ rake db:create

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: rails generate controller home index

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: Hello, Rails!

9 Setting your application homepage Open: config/routes.rb $ rm public/index.html # You can have the root of your site routed with "root" # just remember to delete public/index.html. # root :to => 'welcome#index' root :to => "home#index"

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: rails generate scaffold Post name:string title:string content:text

12 What Scaffold generator created db/migrate/20100207214725_create_pos ts.rb Migration to create the posts table in your database (your name will include a different timestamp) app/models/post.rbThe Post model test/unit/post_test.rbUnit testing harness for the posts model test/fixtures/posts.ymlSample posts for use in testing config/routes.rb Edited to include routing information for posts app/controllers/posts_controller.rbThe Posts controller app/views/posts/index.html.erbA view to display an index of all posts app/views/posts/edit.html.erbA view to edit an existing post app/views/posts/show.html.erbA view to display a single post app/views/posts/new.html.erbA view to create a new post

13 What Scaffold generator created app/views/posts/_form.html.erb A partial to control the overall look and feel of the form used in edit and new views test/functional/posts_controller_test.rb Functional testing harness for the posts controller app/helpers/posts_helper.rb Helper functions to be used from the post views test/unit/helpers/posts_helper_test.rbUnit testing harness for the posts helper app/assets/javascripts/posts.js.coffeeCoffeeScript for the posts controller app/assets/stylesheets/posts.css.scss Cascading style sheet for the posts controller app/assets/stylesheets/scaffolds.css.scss Cascading style sheet to make the scaffolded views look better

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 http://guides.rubyonrails.org/migrations.html rake db:migrate

16 Add a link app/views/home/index.html.erb Hello, Rails!

17 The Model Check app/models/post.rb The Post class inherits from ActiveRecord::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_callb acks.html#validations-overview class Post < ActiveRecord::Base attr_accessible :content, :name, :title validates :name, :presence => true validates :title, :presence => true, :length => { :minimum => 5 } end

20 Test validation $ rails console >> p = Post.new(:content => "A new post") => #<Post id: nil, name: nil, title: nil, content: "A new post", created_at: nil, updated_at: nil> >> p.errors.full_messages => ["Name can't be blank", "Title can't be blank", "Title is too short (minimum is

21 Listing all posts Open the file app/controllers/posts_controller.rb Layouts and rendering: http://guides.rubyonrails.org/layouts_and_rendering.html def index @posts = Post.all respond_to do |format| format.html # index.html.erb format.json { render :json => @posts } end

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_for block: 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://guides.rubyonrails.org/getting_started. html http://ruby.railstutorial.org/chapters/a-demo- app#top http://ruby.railstutorial.org/chapters/a-demo- app#top http://www.randomhacks.net/articles/2007/0 1/20/13-ways-of-looking-at-a-ruby-symbol http://www.randomhacks.net/articles/2007/0 1/20/13-ways-of-looking-at-a-ruby-symbol


Download ppt "Ruby on Rails Your first app. Rails files app/ Contains the controllers, models, views and assets for your application. You’ll focus on this folder for."

Similar presentations


Ads by Google