#262 open
Anton Ageev

Behavior of view helper is incorrect if URL includes "@"

Reported by Anton Ageev | January 29th, 2009 @ 08:10 AM

Given: * Rails 2.1.2 * will_paginate 2.3.6

config/routes.rb:


ActionController::Routing::Routes.draw do |map|
  RE_USER_LOGIN = /[^\/]+/

  map.with_options :controller => "user_blogs", :action => "index" do |r|
    r.connect "blogs/:user_login/pages/:page", :requirements => { :user_login => RE_USER_LOGIN }
    r.connect "blogs/:user_login", :requirements => { :user_login => RE_USER_LOGIN }
  end
  ....
end

app/views/user_blogs/index.html.erb:


  ...
  <%= will_paginate @news %>
  ...

I get page links "/blogs/2Andrex/pages/@", "/blogs/3Andrex/pages/@" when I visited "/blogs/@Andrex".

The routing works fine:


$ ./script/console
>> irb ActionController::Routing::Routes
irb: warn: can't alias context from irb_context.

>> recognize_path "/blogs/@Andrex/pages/2"
=> {:user_login=>"@Andrex", :controller=>"user_blogs", :page=>"2", :action=>"index"}

>> generate :user_login=>"@Andrex", :controller=>"user_blogs", :page=>"2", :action=>"index"
=> "/blogs/@Andrex/pages/2"

Comments and changes to this ticket

  • Mislav

    Mislav January 29th, 2009 @ 11:58 AM

    • State changed from “new” to “resolved”

    That is because of the internal "optimization" when generating URLs.

    You can either use will_paginate 3.0 (not yet released, find it in the "agnostic" branch) or this monkeypatch:

    
    WillPaginate::LinkRenderer.class_eval do
      protected
    
      def url_for(page)
        url_params = {}
        # page links should preserve GET parameters
        stringified_merge url_params, @template.params if @template.request.get?
        stringified_merge url_params, @options[:params] if @options[:params]
        
        if complex = param_name.index(/[^\w-]/)
          page_param = (defined?(CGIMethods) ? CGIMethods : ActionController::AbstractRequest).
            parse_query_parameters("#{param_name}=#{page}")
          
          stringified_merge url_params, page_param
        else
          url_params[param_name] = page
        end
    
        @template.url_for(url_params)
      end
    
    end
    
  • Mislav

    Mislav January 29th, 2009 @ 12:03 PM

    • State changed from “resolved” to “open”

    On the other hand, lets rather leave this ticket open.

  • Anton Ageev

    Anton Ageev January 29th, 2009 @ 03:58 PM

    The monkeypatch brokes all page links. I get "/blogs/@Andrex" instead of "/blogs/@Andres/pages/(\d+)" for any page.

  • Mislav

    Mislav January 29th, 2009 @ 08:26 PM

    Ouch. Then how about

    
    WillPaginate::LinkRenderer.class_eval do
      protected
    
      def url_for(page)
        url_params = {}
        # page links should preserve GET parameters
        stringified_merge url_params, @template.params if @template.request.get?
        stringified_merge url_params, @options[:params] if @options[:params]    
        url_params[param_name.to_sym] = page
    
        @template.url_for(url_params)
      end
    
    end
    
  • Anton Ageev

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป

Everyone's favorite Ruby library for pagination of practically anything!

People watching this ticket

Pages