Beruflich Dokumente
Kultur Dokumente
[ ROR APPLICATION ]
Sanjay kaushik MCA,5th sem
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
Bookmark Model
Our bookmark model will (initially) need two properties URL Title
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
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
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