I’ve got an application that will have a handful of lookup tables. To
be clear by this I mean tables that only have two attributes, id, name
and are used to provide drop-down lists for other tables/forms so that
these fields have “standard” values. I need 5 to 10 of these tables.
I’m a ruby/rails neophyte but I’ve read Agile Web Devel with
Rails, Rails Recipes, Adv Rails Recipes so I think I understand the
basics but this has me stumped. The models/views/controllers for all
these lookup tables are identical. I would think there would be a way
to have 1 Controller (e.g. LookupController) 1 set of views (in
directory /lookups) and a model for each lookup table. Then the Lookup
controller would (possibly with a before_filter) figure out which
lookuptable I want to edit (possibly with a route such as
(lookup/:model) where :model would be the name of the lookup table I
want to edit.
Can anyone point me to either some documentation or provide hints
on how to do this sort of thing cleanly?
Below is the code I’m playing around with…
thanks
in routes I’ve got
lookups/CollectionType/lookups
#lookups/Gender/lookups
map.resources :lookups, :path_prefix => ‘lookups/:mdl’
ex Lookup Controller
class LookupsController < ApplicationController
GET /lookup
GET /lookup.xml
before_filter :setup_tables
def index
@lookups = @theModel.find(:all)
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @lookups }
end
end
GET /lookup/1
GET /lookup/1.xml
def show
@lookup = @theModel.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @lookup }
end
end
GET /lookup/new
GET /lookup/new.xml
def new
@lookup = @theModel.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @lookup }
end
end
GET /lookup/1/edit
def edit
@lookup = @theModel.find(params[:id])
end
POST /lookup
POST /lookup.xml
def create
@lookup = @theModel.new(params[:lookup])
respond_to do |format|
if @lookup.save
flash[:notice] = 'Lookup value was successfully created.'
format.html { redirect_to(@lookup,:mdl=>@mdl) }
format.xml { render :xml => @lookup, :status
=> :created, :location => @lookup }
else
format.html { render :action => “new” }
format.xml { render :xml => @lookup.errors, :status
=> :unprocessable_entity }
end
end
end
PUT /lookup/1
PUT /lookup/1.xml
def update
@lookup = @theModel.find(params[:id])
respond_to do |format|
if @lookup.update_attributes(params[:lookup])
flash[:notice] = 'Lookup was successfully updated.'
format.html { redirect_to(@lookup,:mdl=>@mdl) }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @lookup.errors, :status
=> :unprocessable_entity }
end
end
end
DELETE /lookup/1
DELETE /lookup/1.xml
def destroy
@lookup = @theModel.find(params[:id])
@lookup.destroy
respond_to do |format|
format.html { redirect_to(lookups_url, :mdl=>@mdl) }
format.xml { head :ok }
end
end
private
def setup_tables
# I assume there is some way to instantiate a class with a
string name but I did this
# just to try to get something working
if params[:mdl] == “CollectionType”
@mdl = params[:mdl]
@theModel = CollectionType
else if params[:mdl] == “Gender”
@mdl = params[:mdl]
@theModel = Gender
end
@lookupTbl = @theModel.find(:all)
end
end