Hi All,
I took this idea Christian Hellsten and Jarkko L.'s “Beginning Ruby
on Rails E-Commerce”.
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 = Address.new
end
def create
@address = Address.new(params[:address])
if @address.save
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:
AddressesController<<ApplicationController
def new
load_data(:City)
load_data(:State)
@address = Address.new
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
Thanks,
Mike