Sie sind auf Seite 1von 17

DEMO

[ ROR APPLICATION ]
Sanjay kaushik MCA,5th sem

Starting the Bookmarks Application


Generate application > rails bookmarks

Starting Rails
> cd /directorypath/bookmarks Start the server > ruby script/server start Default environment is development Default port is 3000 http://127.0.0.1:3000
3

Welcome Aboard - Now What?


Hello world seems appropriate >ruby script/generate controller hello exists app/controllers/ exists app/helpers/ create app/views/hello exists test/functional/ create app/controllers/hello_controller.rb create test/functional/hello_controller_test.rb create app/helpers/hello_helper.rb

Mapping URLs to Controllers and Actions


http://127.0.0.1:3000/hello/sayit http://127.0.0.1:3000 - address and port of the webserver hello - name of the controller sayit - name of the action (method in controller)
5

Editing the Controller


def sayit render :text => "<h2>Hello World!</h2>" end

Now for an actual Bookmark


Edit config/database.yml development: adapter: mysql database: bookmarks_development username: username password: password host: localhost
7

Create the Database


This step depends on the database and dba tool of your choice. Create a new schema/dbname for bookmarks_development, and assign rights for the user you listed in database.yml.

Bookmark Model
Our bookmark model will (initially) need two properties URL Title

Scaffolding for Bookmarks


Rails can generate all the basic CRUD operations for simple models via scaffolding. Scaffolding is temporary way to get applications wired quickly.

> ruby script/generate scaffold_resource bookmark url:string title:string


10

Migrations
Rails uses migrations to version the database. Rails tries to minimize SQL at every opportunity Migrations are automatically created whenever you generate a new model Migration files are located in db/migrations The version number is stored in a table called schema_info
11

Bookmarks Migration
located in db/migrate/001_create_bookmarks.rb
class CreateBookmarks < ActiveRecord::Migration def self.up create_table :bookmarks do |t| t.column :url, :string t.column :title, :string end end def self.down drop_table :bookmarks end end

12

Running the Migration


Rake is the general purpose build tool for rails, much like make, or ant. It has many functions, one of which is to control migrations. >rake db:migrate

Now the table has been created

13

Bookmarks Table ID
Bookmarks table has the following fields - id, url, and title Where did the id field come from? Convention of configuration - Rails automatically creates an id field for each new table and uses it as the primary key

14

Bookmarks Controller
The /app/controllers/bookmarks.rb default action: def index @bookmarks = Bookmark.find(:all)

respond_to do |format| format.html # index.rhtml format.xml { render :xml => @bookmarks.to_xml } end End

15

Mapping URLs to Controllers and Actions


http://127.0.0.1:3000/bookmarks/ http://127.0.0.1:3000 - address and port of the webserver hello - name of the controller

/ - name of the action (blank maps to the index action)


16

Bookmarks View
Located in views/bookmarks/index.rhtml
<% for bookmark in @bookmarks %> <tr> <td><%=h bookmark.url %></td> <td><%=h bookmark.title %></td> <td><%=h bookmark.description %></td> <td><%= link_to 'Show', bookmark_path(bookmark) %></td> <td><%= link_to 'Edit', edit_bookmark_path(bookmark) %></td> <td><%= link_to 'Destroy', bookmark_path(bookmark), :confirm => 'Are you sure?', :method => :delete %></td> </tr> <% end %>

17

Das könnte Ihnen auch gefallen