public
Created

  • Download Gist
client model
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
class Client < ActiveRecord::Base
has_many :contacts
has_many :projects
delegate :contacts, :to => :client, :allow_nil => true
#accepts_nested_attributes_for :contacts
validates :organisation_name, :presence => true
validates :address, :presence => true
validates :telephone, :presence => true
validates :email, :presence => true
end
clients controller
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
class ClientsController < ApplicationController
def index
@clients = Client.all
end
def show
@client = Client.find(params[:id])
end
def new
@client = Client.new
end
def create
@client = Client.new(params[:client])
 
if @client.save
flash[:success] = 'Client was successfully created.'
redirect_to @client
else
flash.now[:error] = 'Client was unable to be created.'
render :new
end
end
def edit
@client = Client.find(params[:id])
end
def update
@client = Client.find(params[:id])
 
if @client.update_attributes(params[:client])
flash[:success] = 'Client was successfully updated.'
redirect_to @client
else
flash.now[:error] = 'Client was unable to be updated.'
render :edit
end
end
def destroy
@client = Client.find(params[:id])
@client.destroy
flash[:success] = 'Client was successfully deleted'
redirect_to clients_url
end
end
contact model
1 2 3 4 5 6 7 8 9 10 11 12
class Contact < ActiveRecord::Base
belongs_to :client
has_many :projects
delegate :organisation_name, :to => :client, :allow_nil => true
validates :forename, :presence => true
validates :surname, :presence => true
validates :email, :presence => true
end
contacts controller
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
class ContactsController < ApplicationController
def index
@contacts = Contact.all
end
def show
@contact = Contact.find(params[:id])
end
def new
@contact = Contact.new
end
def create
@contact = Contact.new(params[:contact])
 
if @contact.save
flash[:success] = 'Contact was successfully created.'
redirect_to @contact
else
flash.now[:error] = 'Unable to create contact.'
render :new
end
end
def edit
@contact = Contact.find(params[:id])
end
def update
@contact = Contact.find(params[:id])
 
if @contact.update_attributes(params[:contact])
flash[:success] = 'Contact was successfully updated.'
redirect_to @contact
else
render :edit
end
end
def destroy
@contact = Contact.find(params[:id])
@contact.destroy
flash[:success] = 'Contact was successfully deleted'
redirect_to contacts_url
end
end
index view
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
<div class="page-header">
<h1>All clients</h1>
</div>
 
<div class="well">
<%= link_to 'Add new client', new_client_path, :class => 'btn' %>
</div>
 
<table class="bordered-table zebra-striped">
<thead>
<tr>
<th>Organisation name</th>
<th>Address</th>
<th>Telephone</th>
<th>Email</th>
<th>Website</th>
<th>Twitter handle</th>
<th>Facebook</th>
<th>Contacts</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<% @clients.each do |client| %>
<tr>
<td><%= link_to client.organisation_name, client_path(client) %></td>
<td><%= client.address %></td>
<td><%= link_to client.telephone, 'callto:' + client.telephone %></td>
<td><%= link_to client.email, 'mailto:' + client.email %></td>
<td><%= link_to client.website, 'http://' + client.website %></td>
<td><%= link_to '@' + client.twitter_handle, 'http://www.twitter.com/' + client.twitter_handle %></td>
<td><%= link_to client.facebook, 'http://www.facebook.com/'+ client.facebook %></td>
<td>
<ul>
<% client.contacts.each do |contact| %>
<li><%= contact.forename %></li>
<% end %>
</ul>
</td>
<td>
<%= link_to "Edit", edit_client_path(client), :class => 'btn small' %>
<%= link_to "Delete", client, :confirm => 'Are you sure?', :method => :delete, :class => 'btn small danger' %>
</td>
</tr>
<% end %>
</tbody>
</table>

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.