Skip to content

Instantly share code, notes, and snippets.

Created January 16, 2010 17:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/278902 to your computer and use it in GitHub Desktop.
Save anonymous/278902 to your computer and use it in GitHub Desktop.
# Our Model
class Message < ActiveRecord::Base
validates_presence_of :name, :subject, :body
validates_format_of :email, :with => /^(\S+)@(\S+)\.(\S+)$/
end
# Our Controller
class MessagesController < ApplicationController
# GET /messages
# GET /messages.xml
def index
@messages = Message.all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @messages }
end
end
# GET /messages/1
# GET /messages/1.xml
def show
@message = Message.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @message }
end
end
# GET /messages/new
# GET /messages/new.xml
def new
@message = Message.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @message }
end
end
# GET /messages/1/edit
def edit
@message = Message.find(params[:id])
end
# POST /messages
# POST /messages.xml
def create
@message = Message.new(params[:message])
respond_to do |format|
if @message.save
flash[:notice] = 'Message was successfully created.'
format.html { redirect_to(@message) }
format.xml { render :xml => @message, :status => :created, :location => @message }
else
format.html { render :action => "new" }
format.xml { render :xml => @message.errors, :status => :unprocessable_entity }
end
end
end
# PUT /messages/1
# PUT /messages/1.xml
def update
@message = Message.find(params[:id])
respond_to do |format|
if @message.update_attributes(params[:message])
flash[:notice] = 'Message was successfully updated.'
format.html { redirect_to(@message) }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @message.errors, :status => :unprocessable_entity }
end
end
end
# DELETE /messages/1
# DELETE /messages/1.xml
def destroy
@message = Message.find(params[:id])
@message.destroy
respond_to do |format|
format.html { redirect_to(messages_url) }
format.xml { head :ok }
end
end
end
# this is the database migration file
class CreateMessages < ActiveRecord::Migration
def self.up
create_table :messages do |t|
t.string :name
t.string :company
t.string :phone
t.string :email
t.string :subject
t.text :body
t.timestamps
end
end
def self.down
drop_table :messages
end
end
ActionController::Routing::Routes.draw do |map|
map.resources :messages
map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment