Custom method in Application Controller

Here is the situation. I will be using the "options_from_collection_for_select" and "collection_select" form helper methods in my view to render drop-down selection lists for City and State that have corresponding city_id and state_id foreign key references in my address model/table.

In Chapter 3 of this book, the authors suggesting using a private method in the address controller (contrived) called "load_data" which looks something like this:

def load_data @cities = City.find(:all) @states = State.find(:all) end

This method initializes and loads the @cities and @states instances variables with a hash of all the cities and all the states in their respective tables.

e.g: @cities = {:id => 1, :name => "City Name"}

then, the method is called in the "new", "edit" and "create" methods of the address controller like so...

def new load_data @address = end

def create @address =[:address]) if flash[:notice] = "Address was successfully saved" redirect_to :action => :list else load_data render :action => :new end

but, what I would like to do refactor this by moving this method to the application controller and add parameters to the "load_data" method to preferentially select the model so that I may use this method in other models that have nothing to do with the City or State models and I had hoped it would look something like this:

def load_data(model) @model.pluralize = Model.find(:all) end

and I call the method like so:


def new load_data(:City) load_data(:State) @address = end

but I get a localjumperror exception thrown in the "new" method in the Address controller with the error message "no block given"

does any one have any idea on how to rewrite this method so that I can call it from any controller and initialize a hash that references the model I pass as a parameter

Something like this:

def load_data(model)    instance_variable_set(model.to_s.pluralize.to_sym,                          const_get(model).find(:all) end

Should do it. I haven't tried it though, but something along those lines. If I were you I'd call it using lowercase symbols though. That way you'll end up with @cities and @states.