Skip to content
Snippets Groups Projects
Commit 1e0f1766 authored by Per Cederqvist's avatar Per Cederqvist
Browse files

Scaffold for accounts

parent be7471bb
No related branches found
No related tags found
No related merge requests found
class AccountsController < ApplicationController
def index
list
render :action => 'list'
end
# GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
verify :method => :post, :only => [ :destroy, :create, :update ],
:redirect_to => { :action => :list }
def list
@account_pages, @accounts = paginate :accounts, :per_page => 10
end
def show
@account = Account.find(params[:id])
end
def new
@account = Account.new
end
def create
@account = Account.new(params[:account])
if @account.save
flash[:notice] = 'Account was successfully created.'
redirect_to :action => 'list'
else
render :action => 'new'
end
end
def edit
@account = Account.find(params[:id])
end
def update
@account = Account.find(params[:id])
if @account.update_attributes(params[:account])
flash[:notice] = 'Account was successfully updated.'
redirect_to :action => 'show', :id => @account
else
render :action => 'edit'
end
end
def destroy
Account.find(params[:id]).destroy
redirect_to :action => 'list'
end
end
module AccountsHelper
end
<%= error_messages_for 'account' %>
<!--[form:account]-->
<p><label for="account_name">Name</label><br/>
<%= text_field 'account', 'name' %></p>
<!--[eoform:account]-->
<h1>Editing account</h1>
<%= start_form_tag :action => 'update', :id => @account %>
<%= render :partial => 'form' %>
<%= submit_tag 'Edit' %>
<%= end_form_tag %>
<%= link_to 'Show', :action => 'show', :id => @account %> |
<%= link_to 'Back', :action => 'list' %>
<h1>Listing accounts</h1>
<table>
<tr>
<% for column in Account.content_columns %>
<th><%= column.human_name %></th>
<% end %>
</tr>
<% for account in @accounts %>
<tr>
<% for column in Account.content_columns %>
<td><%=h account.send(column.name) %></td>
<% end %>
<td><%= link_to 'Show', :action => 'show', :id => account %></td>
<td><%= link_to 'Edit', :action => 'edit', :id => account %></td>
<td><%= link_to 'Destroy', { :action => 'destroy', :id => account }, :confirm => 'Are you sure?', :post => true %></td>
</tr>
<% end %>
</table>
<%= link_to 'Previous page', { :page => @account_pages.current.previous } if @account_pages.current.previous %>
<%= link_to 'Next page', { :page => @account_pages.current.next } if @account_pages.current.next %>
<br />
<%= link_to 'New account', :action => 'new' %>
<h1>New account</h1>
<%= start_form_tag :action => 'create' %>
<%= render :partial => 'form' %>
<%= submit_tag "Create" %>
<%= end_form_tag %>
<%= link_to 'Back', :action => 'list' %>
<% for column in Account.content_columns %>
<p>
<b><%= column.human_name %>:</b> <%=h @account.send(column.name) %>
</p>
<% end %>
<%= link_to 'Edit', :action => 'edit', :id => @account %> |
<%= link_to 'Back', :action => 'list' %>
<html>
<head>
<title>Accounts: <%= controller.action_name %></title>
<%= stylesheet_link_tag 'scaffold' %>
</head>
<body>
<p style="color: green"><%= flash[:notice] %></p>
<%= @content_for_layout %>
</body>
</html>
require File.dirname(__FILE__) + '/../test_helper'
require 'accounts_controller'
# Re-raise errors caught by the controller.
class AccountsController; def rescue_action(e) raise e end; end
class AccountsControllerTest < Test::Unit::TestCase
fixtures :accounts
def setup
@controller = AccountsController.new
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
end
def test_index
get :index
assert_response :success
assert_template 'list'
end
def test_list
get :list
assert_response :success
assert_template 'list'
assert_not_nil assigns(:accounts)
end
def test_show
get :show, :id => 1
assert_response :success
assert_template 'show'
assert_not_nil assigns(:account)
assert assigns(:account).valid?
end
def test_new
get :new
assert_response :success
assert_template 'new'
assert_not_nil assigns(:account)
end
def test_create
num_accounts = Account.count
post :create, :account => {}
assert_response :redirect
assert_redirected_to :action => 'list'
assert_equal num_accounts + 1, Account.count
end
def test_edit
get :edit, :id => 1
assert_response :success
assert_template 'edit'
assert_not_nil assigns(:account)
assert assigns(:account).valid?
end
def test_update
post :update, :id => 1
assert_response :redirect
assert_redirected_to :action => 'show', :id => 1
end
def test_destroy
assert_not_nil Account.find(1)
post :destroy, :id => 1
assert_response :redirect
assert_redirected_to :action => 'list'
assert_raise(ActiveRecord::RecordNotFound) {
Account.find(1)
}
end
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment