Forum: Ruby on Rails Hash flattening on paginator link_to

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.
chris landry (Guest)
on 2006-06-07 15:45
(Received via mailing list)
Hey

I need some help with pagination and the link_to in the view.

I'm trying to include a hash in the pagination link_to params and it
keeps flattening out my hash.

Example:
Here's my hash called @answer:
  "answer"=>{"6"=>"9bf31c7ff062936a96d3c8bd1f8f2ff3",
           "7"=>"6f4922f45568161a8cdf4ad2299f6d23"}

Here's the code in my view for my pagination link:
  	<%= link_to('next', {:params => params.merge('answer' => @answer,
	'page' => object_pages.current.previous,)}) + ' ' if
object_pages.current.previous %>

See how the link_to doesn't maintain the actual hash:
<a href="/student_portal/evaluate_exam/7?commit=Submit+Test&amp;
		answer=69bf31c7ff062936a96d3c8bd1f8f2ff376f4922f45568161a8cdf4ad2299f6d23&amp;page=2">next</a>&nbsp;

Anyone know how I can pass a hash intact through a link_to ?

Thanks a ton!
Chris
Ezra Z. (Guest)
on 2006-06-07 21:03
(Received via mailing list)
On Jun 7, 2006, at 3:55 AM, chris landry wrote:

>            "7"=>"6f4922f45568161a8cdf4ad2299f6d23"}
> d23&amp;page=2">next</a>&nbsp;
> http://lists.rubyonrails.org/mailman/listinfo/rails
Hey Chris-

	To get a hash through in an url like that you need to serialize it
and deserialize it. Something like this:

#in your application controller somewhere:

require 'base64'
def construct_for_link(object)
    Base64.encode64(Marshal.dump(object))
end
helper_method : construct_for_link

def deconstruct_from_link(object)
   Marshal.load(Base64.decode64(object))
end
helper_method :deconstruct_from_link


# then in your view
<%= link_to('next', {:params => params.merge('answer' =>
construct_for_link(@answer),
	'page' => object_pages.current.previous,)}) + ' ' if
object_pages.current.previous %>

# in controller
   answer = deconstruct_from_link(params[:answer])


Cheers-
-Ezra
Julian 'Julik' Tarkhanov (Guest)
on 2006-06-07 23:16
(Received via mailing list)
On 7-jun-2006, at 19:02, Ezra Z. wrote:

>> Example:
>> <a href="/student_portal/evaluate_exam/7?commit=Submit+Test&amp;
>>
>> answer=69bf31c7ff062936a96d3c8bd1f8f2ff376f4922f45568161a8cdf4ad2299f
>> 6d23&amp;page=2">next</a>&nbsp;
>>
>> Anyone know how I can pass a hash intact through a link_to ?

or just http://dev.rubyonrails.org/ticket/4947

--
Julian 'Julik' Tarkhanov
please send all personal mail to
me at julik.nl
This topic is locked and can not be replied to.