Skip to content

Instantly share code, notes, and snippets.

@jasonlyles
Created October 16, 2009 18:30
Show Gist options
  • Save jasonlyles/211954 to your computer and use it in GitHub Desktop.
Save jasonlyles/211954 to your computer and use it in GitHub Desktop.
class RouteTable
attr_accessor :table
end
class ServersController < ApplicationController
# GET /servers
# GET /servers.xml
before_filter :login_required, :except => [:new, :edit, :create, :update]
protect_from_forgery :except => [:new, :edit, :create, :update]
def index
require 'will_paginate'
@servers = Server.find(:all).paginate(:page => params[:page],:per_page => 25)
respond_to do |format|
format.html
format.xml { render :xml => @servers }
end
end
# GET /servers/1
# GET /servers/1.xml
def show
@server = Server.find(params[:id])
#This begin/rescue ought to avoid any objects that didn't get marshalled correctly
#1st get the route table
begin
@server.route_table = Marshal.load(@server.route_table)
#Trying here to get the routing table close to the results you'd get when using the command "route print"
@server.route_table.table = @server.route_table.table.sort{|k,v|k[0] <=> v[0]}
rescue TypeError
#Basically setting the route_tables table attribute to nil, so the view can determine that there are no route tables to display
@server.route_table = RouteTable.new
end
#Then get the persistent route table
begin
@server.persistent_route_table = Marshal.load(@server.persistent_route_table)
#Trying here to get the routing table close to the results you'd get when using the command "route print"
@server.persistent_route_table.table = @server.persistent_route_table.table.sort{|k,v|k[0] <=> v[0]}
rescue TypeError
#Basically setting the route_tables table attribute to nil, so the view can determine that there are no route tables to display
@server.persistent_route_table = RouteTable.new
end
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @server }
end
end
# GET /servers/new
# GET /servers/new.xml
def new
@server = Server.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @server }
end
end
# GET /servers/1/edit
def edit
@server = Server.find(params[:id])
end
# POST /servers
# POST /servers.xml
def create
@server = Server.new(params[:server])
respond_to do |format|
if @server.save
flash[:notice] = 'Server was successfully created.'
format.html { redirect_to(@server) }
format.xml { render :xml => @server, :status => :created, :location => @server }
else
format.html { render :action => "new" }
format.xml { render :xml => @server.errors, :status => :unprocessable_entity }
end
end
end
# PUT /servers/1
# PUT /servers/1.xml
def update
@server = Server.find(params[:id])
respond_to do |format|
if @server.update_attributes(params[:server])
flash[:notice] = 'Server was successfully updated.'
format.html { redirect_to(@server) }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @server.errors, :status => :unprocessable_entity }
end
end
end
# DELETE /servers/1
# DELETE /servers/1.xml
def destroy
@server = Server.find(params[:id])
@server.destroy
respond_to do |format|
format.html { redirect_to(servers_url) }
format.xml { head :ok }
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment