Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

url_for failing with singular resource

#4168

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?

Reported by Daniel Lopes · March 13th, 2010 @ 10:48 PM

State: invalid
Milestone: 3.0.2
Assigned to: josh josh
Importance: High

Activity

  1. Ryan Bigg
    Ryan Bigg

    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.

    March 14th, 2010 @ 02:09 AM

  2. José Valim
    José Valim
    • Assigned user changed from José Valim to josh
    • Milestone cleared.

    March 14th, 2010 @ 08:05 AM

  3. Daniel Lopes
    Daniel Lopes

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

    March 14th, 2010 @ 05:42 PM

  4. Daniel Lopes
    Daniel Lopes

    To be more specific the options passed to url_for is:

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

    March 14th, 2010 @ 05:43 PM

  5. José Valim
    José Valim

    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.

    March 14th, 2010 @ 05:47 PM

  6. Daniel Lopes
    Daniel Lopes

    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?

    March 14th, 2010 @ 08:28 PM

  7. José Valim
    José Valim

    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.

    March 14th, 2010 @ 09:03 PM

  8. josh
    josh
    • 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.

    March 15th, 2010 @ 02:52 PM

  9. Jeremy Kemper
    Jeremy Kemper
    • Milestone set to 3.0.2
    • Importance changed from to High

    October 15th, 2010 @ 11:01 PM