views:

41

answers:

1

Hi,

I am trying to set up my routes for the will paginate plugin so I don't have ?page=1 at the end of the url, and so I can later try to use page caching.

I've been browsing around online and I found a few tutorials explain to use map.connect, however, I am having trouble getting it to work with my application.

Here's an example url: http://localhost:3000/profile/1/browse?page=1

Here's the routes code I've got so far:

map.connect '/profile/:id/browse/:page', 
:controller => 'profiles',
:action => 'browse',
:id => /\d+/,
:page => /\d+/

This doesn't work. Does anyone have any advice?

I thought map.connect was pattern matching, but maybe I am missing something.

Thank you,

A: 

Looking at your route, you might be crossing two different things. What are you paginating? If it's profiles, there's no need to supply an id. Let's assume you're trying to paginate profiles. Your route would look like this:

map.connect '/profiles/browse/:page', 
  :controller => 'profiles',
  :action => 'index',
  :page => /\d+/

And your controller action would look like this:

def index
  @profiles = Profile.paginate :page => params[:page]
end

If you are trying to nest something within profiles, say browsing a profile's pictures, you'll need to do it more like this:

map.connect '/profiles/:id/browse/:page', 
  :controller => 'profiles',
  :action => 'index',
  :id => /\d+/,
  :page => /\d+/

with your controller like so:

def index
  @profile = Profile.find(params[:id])
  @pictures = @profile.pictures.paginate :page => params[:page]
end

Let me know if this works.

UPDATE:

You listed in the comments that /profile/1/ is referring to the user's own profile. First, this is dangerous because you don't want people to change what profile the app thinks they are, just by changing that id number by hand. Rely on whatever current_user method your authentication gives you.

However, using your current setup as the example, this is what it would look like:

map.connect '/profiles/:id/browse/:page', 
  :controller => 'profiles',
  :action => 'browse',
  :id => /\d+/,
  :page => /\d+/

with your controller like so:

def browse
  @profile = Profile.find(params[:id])
  @profiles = Profile.paginate :page => params[:page]
end

Let me know if you still have questions.

UPDATE 2

In order to get a nice link_to with this, change the route to a named route:

map.profile_browse '/profiles/:id/browse/:page', 
  :controller => 'profiles',
  :action => 'browse',
  :id => /\d+/,
  :page => /\d+/

Now you can call link_to like so:

link_to profile_browse_path(:id => 1, :page => 10)
Jaime Bellmyer
Hey, browse is a member of the profiles resource. Not sure if that changes anything?
Brian
So when a user is logged in profile/1 represents the user. profile/1/browse just means the user is in the browse section
Brian
What are they browsing, that needs to be paginated?
Jaime Bellmyer
the list of all profiles on the website
Brian
I updated the answer to reflect what you've asked for.
Jaime Bellmyer
profile/1 is the current session user, so the :id attribute could be profile/2 and so on
Brian
what would the link_to method look like for this code? I can get it to work if I manually type in the url
Brian
I've updated my answer with a link_to example.
Jaime Bellmyer
so for each page I have to manually enter the page number?
Brian
There is a view helper, "<%= will_paginate @items %>". That will create links forward and backward for you. If you want to create your own links though, it's easy enough. A link to the next page would look like "<%= link_to profile_browse_path(:id => current_user.id, :page => (params[:page] || 0).to_i + 1) %>".
Jaime Bellmyer
for some reason using either of the two methods you just listed still result in ?page=2
Brian
Hi Brian, I've given you quite a bit to go on here, but I'm afraid I can't spend any more time on your answer. As I said above, you shouldn't be including the the logged-in user's id in the route to begin with. It's a big security risk. Every major authentication plugin gives you "current_user" or something equivalent, so there's no reason to pass it in through the route to begin with. I wish you luck, but there's only so much I can debug your app remotely without the source code. Good luck :)
Jaime Bellmyer
Well, I appreciate you taking the time to help me. I'll try to change my routes to remove the user id, and see if that helps. Thank you.
Brian