Forum: Ruby on Rails Passing a Record as a Parameter

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.
Greg G. (Guest)
on 2006-04-01 07:26
Consider the following code:

Snippet from my view....

<% for cc in @classified_categories %>
<tr>
<td><%= link_to cc.description, :controller =>'classified_ads', :action
=> 'list', :id =>cc %></td>
</tr>
<% end %>

... end snippet

the relevant bit is :id =>cc

Snippet from my controller...

def list
@classified_category = ClassifiedCategory.new(params[:id])
session[:classified_category_id] = @classified_category.id
session[:classified_category_name] = @classified_category.name

@classified_ad_pages, @classified_ads = paginate :classified_ads,
er_page => 10

end

... end snippet

When I run it, I get the following error on the first line of the list
action for the controller:

undefined method `stringify_keys!' for "1":String

It works fine if I just pass the actual id of the record as a parameter
(:id => cc.id) but I'd like to pass the entire record if that's
possible. Any suggestions?

-Greg
Greg G. (Guest)
on 2006-04-01 17:37
I changed it so that instead of trying to pass the entire record, I just
pass the id and then in the controller, do a

ClassifiedCategory.find(params[:id])

and it works ok.  But this is unnecessary DB i/o if it can be avoided by
just passing the record.

-Greg

Greg Greaves wrote:
> Consider the following code:
>
> Snippet from my view....
>
> <% for cc in @classified_categories %>
> <tr>
> <td><%= link_to cc.description, :controller =>'classified_ads', :action
> => 'list', :id =>cc %></td>
> </tr>
> <% end %>
>
> ... end snippet
>
> the relevant bit is :id =>cc
>
> Snippet from my controller...
>
> def list
> @classified_category = ClassifiedCategory.new(params[:id])
> session[:classified_category_id] = @classified_category.id
> session[:classified_category_name] = @classified_category.name
>
> @classified_ad_pages, @classified_ads = paginate :classified_ads,
> er_page => 10
>
> end
>
> ... end snippet
>
> When I run it, I get the following error on the first line of the list
> action for the controller:
>
> undefined method `stringify_keys!' for "1":String
>
> It works fine if I just pass the actual id of the record as a parameter
> (:id => cc.id) but I'd like to pass the entire record if that's
> possible. Any suggestions?
>
> -Greg
This topic is locked and can not be replied to.