Form and pagination question

Hello,

How do I get params from a form to use with a paginator?

debug(params) shows this
city: !map:HashWithIndifferentAccess
id: “1”

but in my controller, when I try to use
@hotel_pages = Paginator.new self, Hotel.count(:conditions =>
“hotels.city_id = params[:city][:id]”), 3, @params[‘page’]

it doesn’t work. I suppose it’s a simple syntax error but have no idea
to fix it.

Thanks for your help!

but in my controller, when I try to use
@hotel_pages = Paginator.new self, Hotel.count(:conditions =>
“hotels.city_id = params[:city][:id]”), 3, @params[‘page’]

it doesn’t work. I suppose it’s a simple syntax error but have no idea
to fix it.

Nobody?

but in my controller, when I try to use
@hotel_pages = Paginator.new self, Hotel.count(:conditions =>
“hotels.city_id = params[:city][:id]”), 3, @params[‘page’]

Nobody?

@hotel_pages, @hotels = paginate(:hotels, :conditions =>
[“hotels.city_id = ?”, params[:city][:id]], :per_page => 3, :page =>
@params[‘page’])

Try something like that.

Thanks Dr Nic, I replaced :page => @params[‘page’] by :parameter =>
‘page’ and it works but only for the first page, then I have an error
You might have expected an instance of Array.
The error occured while evaluating nil.[]

Any idea?

@hotel_pages = Paginator.new self, Hotel.count(:conditions =>
“hotels.city_id = params[:city][:id]”), 3, @params[‘page’]

@hotel_pages, @hotels = paginate(:hotels, :conditions =>
[“hotels.city_id = ?”, params[:city][:id]], :per_page => 3, :page =>
@params[‘page’])

Try something like that.

Thanks Dr Nic, I replaced :page => @params[‘page’] by :parameter =>
‘page’ and it works but only for the first page, then I have an error
You might have expected an instance of Array.
The error occured while evaluating nil.[]

Try :parameter => params[:page]

Try :parameter => params[:page]

In fact, I think it’s more a hash problem in the link previous / next.

I found that in the archives:

“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])

So the question is: is it the only way to add an hash in a link?

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs