This project is archived and is in readonly mode.

#4168 ✓invalid
Daniel Lopes

url_for failing with singular resource

Reported by Daniel Lopes | March 13th, 2010 @ 10:48 PM | in 3.0.2

I'm not sure it's a bug but the behavior is very strange.

If we use some singular resource like:

map.resource :account

And call form_for or link_to with an instance of Account it generate a very weird url.

<%= url_for Account.first %>

Generates something like: /account.%23%3Caccount:0x103629d60%3E

I should right some test for this bug and send a patch or it's desired behavior?

Comments and changes to this ticket

  • Ryan Bigg

    Ryan Bigg March 14th, 2010 @ 02:09 AM

    I would suggest this is a bug as it is definitely surprising behaviour. I would expect this to return a similar result to polymorphic_path.

    Please submit a test + patch if you could.

  • José Valim

    José Valim March 14th, 2010 @ 08:05 AM

    • Assigned user changed from “José Valim” to “josh”
    • Milestone cleared.
  • Daniel Lopes

    Daniel Lopes March 14th, 2010 @ 05:42 PM

    Why this bug was assigned to milestone 3.0? It shouldn't be released with Rails 2.3.6?

    The problem is with url_for method, when we send a active record object that maps to a singleton resource the url_for try use the instance as format (that's why we have /account.%23%3Caccount:0x103629d60%3E wich is escaped version of /account.<account:object_id>).

  • Daniel Lopes

    Daniel Lopes March 14th, 2010 @ 05:43 PM

    To be more specific the options passed to url_for is:

    {:action=>"show", :controller=>"accounts", :format=>#, :use_route=>:account, :only_path=>false}

  • José Valim

    José Valim March 14th, 2010 @ 05:47 PM

    You haven't told which Rails version the bug occurs. Anyway, since we are working in the 3.0 branch, we have higher priority in fixing it on 3.0. You/we could backport the fix later.

  • Daniel Lopes

    Daniel Lopes March 14th, 2010 @ 08:28 PM

    Thanks Valim.

    About the bug, it happens because url_for call the named route when you send a record to it. In some context I don't think it's wrong, like this:
    account_url #=> "http://www.example.com/account"

    and when you send a record (wich don't make sense in this context):
    account_url Account.first #=> "http://www.example.com/account.%23%3Caccount:0x102bb9fb0%3E"

    But the problem is when you will try to use things like form_for in records that map to singleton resources:

    <% form_for @account do |f| %>
    ... <% end %>

    I'm still not sure it's a bug. What do you think?

  • José Valim

    José Valim March 14th, 2010 @ 09:03 PM

    I would regard this is a bug, although this is a known bug. I guess there is not a simple fix for it on Rails 2.3, so the question really is if Rails 3.0 router can help us on this and if so, how hard would be to backport the fix for Rails 2.3.

    Josh will be able to tell us.

  • josh

    josh March 15th, 2010 @ 02:52 PM

    • State changed from “new” to “invalid”

    This is a known limitation.

    I don't think there is an easy to implement this either. Then we'd have to deal with the case where you have resources and resource :account.

  • Jeremy Kemper

    Jeremy Kemper October 15th, 2010 @ 11:01 PM

    • Milestone set to 3.0.2
    • Importance changed from “” to “High”

Create your profile

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

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Tags

Pages