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.
73606b1d1042de19056f3d59492b652f?d=identicon&s=25 Charlie Bowman (beachguy82)
on 2006-01-04 06: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
73606b1d1042de19056f3d59492b652f?d=identicon&s=25 Charlie Bowman (beachguy82)
on 2006-01-04 06:40
I should specify that I'm calling the funtion with a link <a
href="/store/browse/shoes">
C8a634a01a2c4508360874bff7fb1a7f?d=identicon&s=25 Kevin Olbrich (Guest)
on 2006-01-04 06: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.
73606b1d1042de19056f3d59492b652f?d=identicon&s=25 Charlie Bowman (beachguy82)
on 2006-01-04 07: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!
6828ffc79486cd2442714bf32286a910?d=identicon&s=25 Vivek Krishna (Guest)
on 2006-01-04 12: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
C8a634a01a2c4508360874bff7fb1a7f?d=identicon&s=25 Kevin Olbrich (Guest)
on 2006-01-04 13:38
Vivek Krishna 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.
F66cc63471b71843dba49db8e2b3771d?d=identicon&s=25 Lieven De keyzer (lieven)
on 2006-01-04 13:53
Kevin Olbrich 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?
C8a634a01a2c4508360874bff7fb1a7f?d=identicon&s=25 Kevin Olbrich (Guest)
on 2006-01-04 15: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.