Hi All, I took this idea Christian Hellsten and Jarkko Laine''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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> 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 parameterSomething 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. -philip --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---