# Use helper method in controller?

**URL:** <https://www.ruby-forum.com/t/use-helper-method-in-controller/109858>\
**Category:** Rails\
**Created:** [July 19, 2007, 6:04pm UTC](https://www.ruby-forum.com/t/use-helper-method-in-controller/109858 "2007-07-19T18:04:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![b0rdslide](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a9a28c/32.png) [@b0rdslide](https://www.ruby-forum.com/u/b0rdslide)\
**Post date:** [July 19, 2007, 6:04pm UTC](https://www.ruby-forum.com/t/use-helper-method-in-controller/109858/1 "2007-07-19T18:04:40Z")

</div>

I created a helper method in person\_helper.rb, and want to use this  
method in person\_controller#list, but I get the error “undefined method  
`filter\_sql’ for #PersonController:0x872dec4”.

Here is the helper class (person\_helper.rb):

module PersonHelper  
Sql = “SELECT \* FROM People WHERE name LIKE ? OR address LIKE ?”  
def filter\_sql(name, address, city)  
sanitize\_sql([Sql, name.to\_s + ‘%’, address.to\_s + ‘%’])  
end  
end

Here is the relevant part of the controller class  
(person\_controller.rb):

class PersonController \< ApplicationController  
def list  
sql = filter\_sql(params[:name\_val], params[:address\_val])  
@person\_pages, @people = paginate Person.find\_by\_sql(sql), per\_page  
=\> 50  
end  
end

Any ideas on what I am doing wrong? Thanks in advance for your help!

---

<div class="post-metadata">

**Author:** ![b0rdslide](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a9a28c/32.png) [@b0rdslide](https://www.ruby-forum.com/u/b0rdslide)\
**Post date:** [July 19, 2007, 6:18pm UTC](https://www.ruby-forum.com/t/use-helper-method-in-controller/109858/2 "2007-07-19T18:18:13Z")

</div>

I would highly recommend you push this back into your model if you need  
it  
in both places. This seems like business logic, which belongs in the  
model.

class Person \< ActiveRecord::Base

… your other stuff

def self.filter\_by\_name\_and\_address(name, address)

```
   name = "#{name}%"
   address = "#{address}%"
   self.find(:all, conditions=>["name like ? and address like ?", 

```

name,  
address

end

end

Your controller might be like this now:

def list  
@person\_pages, @people = paginate  
Person.filter\_by\_name\_and\_address(params[:name],  
params[:address]), per\_page =\> 50  
end

That’s untested, btw, but it should get you going in the right  
direction.  
Helpers are really for code that is directly related to views and  
controllers. You want to try to keep as much db stuff in the model as  
you  
can. You might have a compelling reason to use the helper as you’d  
originally intended, so my apologies if this is not suitable for you.

---

<div class="post-metadata">

**Author:** ![b0rdslide](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a9a28c/32.png) [@b0rdslide](https://www.ruby-forum.com/u/b0rdslide)\
**Post date:** [July 20, 2007, 1:34am UTC](https://www.ruby-forum.com/t/use-helper-method-in-controller/109858/3 "2007-07-20T01:34:13Z")

</div>

> Your controller might be like this now:
> 
> def list  
> @person\_pages, @people = paginate  
> Person.filter\_by\_name\_and\_address(params[:name],  
> params[:address]), per\_page =\> 50  
> end

Putting this in the model class is good advice. I am having a problem  
in getting this to work, however, looking for a bit more help…

Here is my model list method (stripped down to isolate my problem):

def list  
@person\_pages, @people = paginate Person.find(:all, :conditions =\>  
“lastname=‘test’”), :per\_page =\> 50  
end

which results in this exception:  
“#Person:0x85f9d14#Person:0x85f9b5c#Person:0x85f9b34#Person:0x85f9b0c#Person:0x85f9ae4#Person:0x85f9abc#Person:0x85f9a94#Person:0x85f9a6c”  
is not a valid constant name!

Apparently using Person.find (like the scaffold show method) doesn’t  
work with paginate??

Any ideas? This seems so simple and is undoubtedly a stupid error on my  
part, but I just don’t see it…

---

<div class="post-metadata">

**Author:** ![b0rdslide](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a9a28c/32.png) [@b0rdslide](https://www.ruby-forum.com/u/b0rdslide)\
**Post date:** [July 20, 2007, 4:15am UTC](https://www.ruby-forum.com/t/use-helper-method-in-controller/109858/4 "2007-07-20T04:15:31Z")

</div>

Yes, sorry… that was a mistake on my end. I should have tested my  
code  
before I sent it to you. The built-in paginator is kinda ugly, doesn’t  
perform well. I’d recommend the will\_paginate plugin. It will let you  
do  
what you want to do. You are correct that the paginate method doesn’t  
let  
you do what you want.

---

<div class="post-metadata">

**Author:** ![b0rdslide](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a9a28c/32.png) [@b0rdslide](https://www.ruby-forum.com/u/b0rdslide)\
**Post date:** [July 20, 2007, 4:35am UTC](https://www.ruby-forum.com/t/use-helper-method-in-controller/109858/5 "2007-07-20T04:35:49Z")

</div>

> I’d recommend the will\_paginate plugin. It will let you  
> do what you want to do.

My last example was stripped down, I will still need to use a  
find\_by\_sql method. I see this is supported with will\_paginate, but the  
:total\_entries parameter must be explicitly provided. Does this mean I  
would have to run a query first to get/provide the total? This would  
also mean that I would have to re-retrieve this value when a row is  
added/deleted but I guess that makes sense since the pages wouldn’t be  
right anymore anyway…
