# Custom method in Application Controller

**URL:** <https://www.ruby-forum.com/t/custom-method-in-application-controller/118364>\
**Category:** Rails\
**Created:** [October 26, 2007, 3:38am UTC](https://www.ruby-forum.com/t/custom-method-in-application-controller/118364 "2007-10-26T03:38:13Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mifi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7993a0/32.png) [@mifi](https://www.ruby-forum.com/u/mifi)\
**Post date:** [October 26, 2007, 3:38am UTC](https://www.ruby-forum.com/t/custom-method-in-application-controller/118364/1 "2007-10-26T03:38:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mifi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7993a0/32.png) [@mifi](https://www.ruby-forum.com/u/mifi)\
**Post date:** [October 26, 2007, 5:43am UTC](https://www.ruby-forum.com/t/custom-method-in-application-controller/118364/2 "2007-10-26T05:43:55Z")

</div>

> def load\_data
> 
> if @address.save  
> models that have nothing to do with the City or State models and I had  
> def new  
> 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.

-philip
