Forum: Ruby on Rails link_to with all properties of a model

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.
82c59780911721f5cbe82d3111313519?d=identicon&s=25 Giant Cranes (giantcranes)
on 2007-01-09 18:03
Hi,

I am trying to create a link with all of the properties of a model as
parameters. Is this possible in a generic way?

Say I have the model:

filter = Filter.new
filter.page_number = 2
filter.page_size = 10
filter.query = 'books'

I want to create a link link:

<a href="some/action/?page_number=2&page_size=10&query=books">

Thanks,
GiantCranes
Ce953bec3af375ddc75e375233112b28?d=identicon&s=25 harper (Guest)
on 2007-01-10 13:28
> @filter = Filter.new
> @filter.page_number = 2
> @filter.page_size = 10
> @filter.query = 'books'

-- untested code. the idea is that @filter.attributes returns a hash
like
@filter.attributes = {:page_number => 2, :page_size => 10, :query =>
'books'}

so, you should aim for something like:

link_to "abc", {:controller => 'some', :action => 'action',
@filter.attributes }

**((you may need to do some hash - manipulation, so you don't get a hash
in a hash (you want it all to be in one hash), but that's the idea...

hth,

shai
Ce953bec3af375ddc75e375233112b28?d=identicon&s=25 harper (Guest)
on 2007-01-10 13:35
hash manipulation could go for :

Hash#update
http://rubycentral.com/book/ref_c_hash.html#Hash.update

something like:

n={:a => 1, :b => 2 }
m={:c => 3}

n.update(m) # gives {:a => 1, :b => 2, :c => 3}

this way you can add the two hashes of {:controller => x,:action => y}
and {:page_number =>2, :other => attributes}

:)

ciao

s
This topic is locked and can not be replied to.