ROR in 14 pages
Here's a quick overview of Ruby on Rails in 14 pages.
Thinking About Things
class Account < ActiveRecord::Base
has_many :users
validates_presence_of :name
validates_uniqueness_of :name
validates_length_of :name, :maximum => 100
end
class User < ActiveRecord::Base
belongs_to :account
validates_presence_of :first_name, :last_name
validates_uniqueness_of :first_name
end
<h1>New account</h1>
<%= start_form_tag :action => 'create' %>
<%= error_messages_for 'account' %>
<!--[form:account]-->
<p><label for="account_name">Name</label><br/>
<%= text_field 'account', 'name' %></p>
<!--[eoform:account]-->
<%= error_messages_for 'user' %>
<!--[form:user]-->
<p><label for="user_first_name">First name</label><br/>
<%= text_field 'user', 'first_name' %></p>
<p><label for="user_last_name">Last name</label><br/>
<%= text_field 'user', 'last_name' %></p>
<!--[eoform:user]-->
<%= submit_tag "Create" %>
<%= end_form_tag %>
<%= link_to 'Back', :action => 'list' %>
class SignupController < ApplicationController
model :user
model :account
def index
new
render :action => 'new'
end
def new
@account = Account.new(@params['account'])
@user= User.new(@params['yser'])
end
def create
@account = Account.new(@params['account'])
@user = User.new(@params['user'])
@account.valid?
@user.valid?
if @account.valid? && @user.valid?
name = @account.name
@account.save
@account = Account.find_by_name(name)
@account.users << @user
flash[:notice] = 'Account was successfully created.'
redirect_to :action => 'new'
else
render :action => 'new'
end
end
end