Forum: Ruby on Rails Passing :id param in link_to etc...

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.
Jean N. (Guest)
on 2007-06-08 18:18

Just need some more inside information...

Sample Code from the AWDWR book.

<%= link_to ( image_tag( product.image_url ),
   { :action => :add_to_cart, :id => product },
   :method => :post,
   :confirm => "Are you sure?" ) %>

Why is my ":id =>" reference using 'product' and not 'product.id'

Is the :id symbol 'dual purpose' and used as a key for an id param AND
it also infers that the FIELD to 'get' from the product object is named
'id'?

Thanks.
unknown (Guest)
on 2007-06-08 18:25
(Received via mailing list)
Hi --

On Fri, 8 Jun 2007, Jean N. wrote:

>   :confirm => "Are you sure?" ) %>
>
> Why is my ":id =>" reference using 'product' and not 'product.id'
>
> Is the :id symbol 'dual purpose' and used as a key for an id param AND
> it also infers that the FIELD to 'get' from the product object is named
> 'id'?

It uses the to_param method to get the value, so product ends up being
product.to_param.  By default, to_param returns the id, but you can
override it if you wish:

   class Book < ActiveRecord::Base
     def to_param
       isbn
     end
   end

for example.


David

--
Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black)
    (See what readers are saying!  http://www.rubypal.com/r4rrevs.pdf)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)
This topic is locked and can not be replied to.