#262 open
Anton Ageev

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

Reported by Anton Ageev | 2009-01-29 08:10:02 UTC

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 2009-01-29 11:58:16 UTC

    • 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 2009-01-29 12:03:45 UTC

    • State changed from “resolved” to “open”

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

  • Anton Ageev

    Anton Ageev 2009-01-29 15:58:20 UTC

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

  • Mislav

    Mislav 2009-01-29 20:26:22 UTC

    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

    Anton Ageev 2009-01-30 08:17:07 UTC

    Thanks. It works fine.

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