# Wrong number of arguments error

**URL:** <https://www.ruby-forum.com/t/wrong-number-of-arguments-error/89282>\
**Category:** Rails\
**Created:** [January 4, 2006, 6:29am UTC](https://www.ruby-forum.com/t/wrong-number-of-arguments-error/89282 "2006-01-04T06:29:17Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![beachguy82](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9dc877/32.png) [@beachguy82](https://www.ruby-forum.com/u/beachguy82)\
**Post date:** [January 4, 2006, 6:29am UTC](https://www.ruby-forum.com/t/wrong-number-of-arguments-error/89282/1 "2006-01-04T06:29:17Z")

</div>

I keep getting this error every time I try to pass a value to a method.  
Here is a sample nonworking method. I’ve been pulling my hair out over  
this all night! Why won’t it work!?

My url path is :3000/store/browse/shoes and I’m getting this in the log  
Parameters: {“action”=\>“browse”, “id”=\>“shoes”, “controller”=\>“store”}

# function to browse the store

def browse (category)  
@product\_pages,@products = paginate(:products,  
:per\_page =\> 12,  
:conditions =\> “status=  
‘current’,  
category =  
#{category}”)  
render :template =\> “store/index”  
end

---

<div class="post-metadata">

**Author:** ![beachguy82](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9dc877/32.png) [@beachguy82](https://www.ruby-forum.com/u/beachguy82)\
**Post date:** [January 4, 2006, 6:40am UTC](https://www.ruby-forum.com/t/wrong-number-of-arguments-error/89282/2 "2006-01-04T06:40:49Z")

</div>

I should specify that I’m calling the funtion with a link

---

<div class="post-metadata">

**Author:** ![beachguy82](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9dc877/32.png) [@beachguy82](https://www.ruby-forum.com/u/beachguy82)\
**Post date:** [January 4, 2006, 6:59am UTC](https://www.ruby-forum.com/t/wrong-number-of-arguments-error/89282/3 "2006-01-04T06:59:47Z")

</div>

charlie bowman wrote:

> I should specify that I’m calling the funtion with a link
> 
> [](https://www.ruby-forum.com/store/browse/shoes)

[ 

For starters, actions don’t take arguments.  
If you want it to be called from a URL, you need to pass arguments in  
params so your URL would look like this…

 

\<%= link\_to ‘Browse’, :controller=\>‘store’, :action=\>‘browse’,  
:id=\>‘shoes’ %\>

 

…  
I’ll assume that you are passing the category through the ‘id’ value…  
…

 

def browse  
@product\_pages,@products = paginate(:products,  
:per\_page =\> 12,  
:conditions =\> [“status=  
‘current’,  
category = ?”,  
params[:id]])  
render :template =\> “store/index”  
end

 

\*\* please also note that directly substituting a parameter string into  
an SQL query is a REALLY BAD idea. Please don’t do this. Think of the  
children.

](https://www.ruby-forum.com/store/browse/shoes)

---

<div class="post-metadata">

**Author:** ![beachguy82](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9dc877/32.png) [@beachguy82](https://www.ruby-forum.com/u/beachguy82)\
**Post date:** [January 4, 2006, 7:10am UTC](https://www.ruby-forum.com/t/wrong-number-of-arguments-error/89282/4 "2006-01-04T07:10:23Z")

</div>

thank you so much! I’ve been trying for hours to figure that out. With  
a small modification to your action it worked perfectly.

def browse  
@product\_pages,@products = paginate(:products,  
:per\_page =\> 12,  
:conditions =\> [“status=  
‘current’ and  
category = ?”,  
params[:id]])  
render :template =\> “store/index”  
end

Thanks again!

---

<div class="post-metadata">

**Author:** ![beachguy82](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9dc877/32.png) [@beachguy82](https://www.ruby-forum.com/u/beachguy82)\
**Post date:** [January 4, 2006, 12:24pm UTC](https://www.ruby-forum.com/t/wrong-number-of-arguments-error/89282/5 "2006-01-04T12:24:48Z")

</div>

Just a question on passing parameters …Rails converts  
a url like  
:controller=\>‘store’,:action=\>‘browse’,:id=\>2  
to something like  
store/browse/2  
Looks like the parameter :id is special in some way. Can we use other  
parameters and if yes are they converted to a urlencoded string sent  
along  
with the url like a GET method in forms ?

for example what if i need something like the value of some variable  
like  
the user’s browser. ( :ua =\> ‘moz’ …) which could be set by a script

Vivek

---

<div class="post-metadata">

**Author:** ![beachguy82](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9dc877/32.png) [@beachguy82](https://www.ruby-forum.com/u/beachguy82)\
**Post date:** [January 4, 2006, 1:38pm UTC](https://www.ruby-forum.com/t/wrong-number-of-arguments-error/89282/6 "2006-01-04T13:38:03Z")

</div>

Vivek K. wrote:

> Just a question on passing parameters …Rails converts  
> a url like  
> :controller=\>‘store’,:action=\>‘browse’,:id=\>2  
> to something like  
> store/browse/2  
> Looks like the parameter :id is special in some way. Can we use other  
> parameters and if yes are they converted to a urlencoded string sent  
> along  
> with the url like a GET method in forms ?
> 
> for example what if i need something like the value of some variable  
> like  
> the user’s browser. ( :ua =\> ‘moz’ …) which could be set by a script
> 
> Vivek

If you add additional parameters that don’t show up in your routing, you  
will get something like this…

[http://www.somesite.com/store/browse/2?ua=moz](http://www.somesite.com/store/browse/2?ua=moz)

Which is a great way to pass additional information in some cases. Just  
remember that the user can also type in lines like this manually, so be  
careful what you do with them.

---

<div class="post-metadata">

**Author:** ![beachguy82](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9dc877/32.png) [@beachguy82](https://www.ruby-forum.com/u/beachguy82)\
**Post date:** [January 4, 2006, 1:53pm UTC](https://www.ruby-forum.com/t/wrong-number-of-arguments-error/89282/7 "2006-01-04T13:53:17Z")

</div>

Kevin O. wrote:

> def browse  
> @product\_pages,@products = paginate(:products,  
> :per\_page =\> 12,  
> :conditions =\> [“status=  
> ‘current’,  
> category = ?”,  
> params[:id]])  
> render :template =\> “store/index”  
> end
> 
> \*\* please also note that directly substituting a parameter string into  
> an SQL query is a REALLY BAD idea. Please don’t do this. Think of the  
> children.

What should be done with this parameter string before inserting it into  
a query? Say you want it to be a number. Check if the string represents  
an integer?

---

<div class="post-metadata">

**Author:** ![beachguy82](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9dc877/32.png) [@beachguy82](https://www.ruby-forum.com/u/beachguy82)\
**Post date:** [January 4, 2006, 3:11pm UTC](https://www.ruby-forum.com/t/wrong-number-of-arguments-error/89282/8 "2006-01-04T15:11:41Z")

</div>

Lieven De Keyzer wrote:

> What should be done with this parameter string before inserting it into  
> a query? Say you want it to be a number. Check if the string represents  
> an integer?

The safe way to insert it would be to use

:conditions =\> [“category = ?”, params[:id]]

This performs some additional checking to avoid insertion of dangerous  
strings.

What it won’t do is validate if the user should be able to actually  
access that record or not.

You could also do a table lookup. Use the parameter as a key to a  
pre-made hash if you only do a couple of lookups. For example…

limits = {‘first’=\>‘firstname, lastname’, ‘last’=\>‘lastname, firstname’}

People.find(:all, :order =\> limits[params[:sort]])

you could call this with a helper like this…

link\_to ‘Sort by lastname’, :action=\>‘action’, :sort=\>‘last’
