开发者

Ruby on Rails will paginate routes

I am trying to set up my routes for the will paginate plugin so I don't have ?page=1 at t开发者_Python百科he 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,


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)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜