Forum: Ruby on Rails wrong number of arguments error

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Charlie B. (Guest)
on 2006-01-04 07:29
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
Charlie B. (Guest)
on 2006-01-04 07:40
I should specify that I'm calling the funtion with a link <a
href="/store/browse/shoes">
Kevin O. (Guest)
on 2006-01-04 07:59
charlie bowman wrote:
> I should specify that I'm calling the funtion with a link <a
> href="/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.
Charlie B. (Guest)
on 2006-01-04 08:10
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!
Vivek K. (Guest)
on 2006-01-04 13:24
(Received via mailing list)
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
Kevin O. (Guest)
on 2006-01-04 14:38
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

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.
Lieven D. (Guest)
on 2006-01-04 14:53
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?
Kevin O. (Guest)
on 2006-01-04 16:11
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'
This topic is locked and can not be replied to.