0% found this document useful (0 votes)
62 views17 pages

(Ror Application) : Sanjay Kaushik MCA, 5 Sem

This document provides instructions for creating a basic bookmarks application using Ruby on Rails. It describes generating the application, starting the server, generating controllers and views, creating a database migration to support a Bookmark model with a URL and title, scaffolding the resource, running the migration, and viewing the bookmarks index page. The application is set up to display, create, edit and delete bookmarks.

Uploaded by

Sanjay Kaushik
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views17 pages

(Ror Application) : Sanjay Kaushik MCA, 5 Sem

This document provides instructions for creating a basic bookmarks application using Ruby on Rails. It describes generating the application, starting the server, generating controllers and views, creating a database migration to support a Bookmark model with a URL and title, scaffolding the resource, running the migration, and viewing the bookmarks index page. The application is set up to display, create, edit and delete bookmarks.

Uploaded by

Sanjay Kaushik
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 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

You might also like