Ruby on Rails Project Setup

From SubfireWiki

Jump to: navigation, search

here's everything I did to setup a rails project:

Contents

First things First

This is based on two posts by Bob Silva:

  • Create a rails skeleton project:
rails jakesruby

Model

  • First let's create a model:
./script/generate model message
  • If this is the first generate command then it will create the basic project structure.
  • Most importanly it will create a model and a migration file:
app/models/messages.rb
class Messages < ActiveRecord::Base
end

Note: if you're coming over from Java, then you can breath, because ActiveRecords are business and data objects rolled into one. And the beatiful thing is, it automagically gives you fields/methods based on the table structure (columns).

  • Modify app/models/messages.rb to add in the automagic updating of parent, left and right:
class CreateMessages < ActiveRecord::Migration
  def self.up
    create_table :messages, :force => true do |t|
      t.column :root_id, :integer, :default => 0
      t.column :parent_id, :integer, :default => 0
      t.column :lft, :integer, :default => 0
      t.column :rgt, :integer, :default => 0
      t.column :depth, :integer, :default => 0
      t.column :subject, :string
      t.column :body, :text
      t.column :name, :string
      t.column :updated_at, :timestamp
      t.column :created_at, :timestamp
      t.column :author_id, :integer
      t.column :category_id, :integer
    end
  end

  def self.down
    drop_table :messages
  end
end
db/migrate/001_create_messages.rb
class CreateMessages < ActiveRecord::Migration
  def self.up
    create_table :messages do |t|
      t.column :name, :string
    end
  end

  def self.down
    drop_table :messages
  end
end
  • modify db/migrate/001_create_messages.rb to add some columns for our message board:
class CreateMessages < ActiveRecord::Migration
  def self.up
    create_table :messages do |t|
      t.column :root_id, :integer
      t.column :parent_id, :integer
      t.column :lft, :integer
      t.column :rgt, :integer
      t.column :depth, :integer
      t.column :subject, :string
      t.column :body, :text
      t.column :name, :string
      t.column :updated_at, :timestamp
      t.column :created_at, :timestamp
      t.column :author_id, :integer
      t.column :category_id, :integer
    end
  end

  def self.down
    drop_table :messages
  end
end

Scaffold

./script/generate scaffold message

View

  • Add a reply to app/views/messages/show.rhtml:
<% for column in Message.content_columns %>
<p>
  <b><%= column.human_name %>:</b> <%=h @message.send(column.name) %>
</p>
<% end %>

<%= link_to 'Reply', :action => 'reply', :id => @message %> |
<%= link_to 'Edit', :action => 'edit', :id => @message %> |
<%= link_to 'Back', :action => 'list' %>
  • Add a hidden field to the form: app/views/messages/_form.rhtml :
<%= error_messages_for 'message' %>

<%= hidden_field 'post', 'parent_id' %>

<!--[form:message]-->
... <snip> ...

Controller

  • modify the controller's list method to add some order; app/controllers/messages_controller.rb :
  def list
    @message_pages, @messages = paginate :messages, :per_page => 10, :order => 'root_id desc, lft'
  end
  • add a reply method to messages_controller.rb :
  def reply
    @message = Message.new
    @message.parent_id = params[:id]
    render :action => 'new'
  end


Subversion

Personal tools