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 documentation - only_path

#2436

The description for url_for: http://api.rubyonrails.org/class...

is slightly wrong, it says:

:only_path - If true, returns the relative URL (omitting the protocol, host name, and port) (false by default).

I tested it on rails 2.2 and rails 2.3 and it is the other way round: it is "true" by default, not false.

E.g. this: url_for(:controller => 'shoppingcarts', :action => 'error')

gives me the relative url /shoppingcarts/error, but this:

url_for(:controller => 'shoppingcarts', :action => 'error', :only_path => false)

gives me the absolute url http://...../shoppingcarts/error

So i think the sentence should read:

:only_path - If true, returns the relative URL (omitting the protocol, host name, and port) (true by default).

Reported by j0llyr0g3r · April 7th, 2009 @ 12:54 AM

State: stale
Milestone: 3.x
Assigned to: nobody
Importance: none

Activity

  1. heidmo
    heidmo
    • Assigned user set to CancelProfileIsBroken

    Yeah I agree, it should say "(true by default)" instead of false.

    April 21st, 2009 @ 02:08 AM

  2. CancelProfileIsBroken
  3. Derek McLoughlin
    Derek McLoughlin

    url_for behaves differently depending on whether it is called from within a view or within a controller.

    From within a view, the default is a relative URL, and :only_path => false gives you the full URL.

    From within a controller, the default is a full URL, and :only_path => true gives you the relative URL.

    Sample code with a scaffold 'Person'

    Controller:
    @@@ruby def show

    @person = Person.find(params[:id])
    
    @person_url = url_for(@person)
    @person_url_only_path_false = url_for(:id => params[:id], :only_path => false)
    @person_url_only_path_true = url_for(:id => params[:id], :only_path => true)
    

    end

    
    show.rhtml
    @@@ruby
    <p>IN THE VIEW</p>
    <p>url_for(@person) = <%= url_for(@person) %></p>
    <p>url_for(:id => 1, :only_path => false) = <%= url_for(:id => 1, :only_path => false) %></p>
    <p>url_for(:id => 1, :only_path => true) = <%= url_for(:id => 1, :only_path => true) %></p>


    <p>FROM THE CONTROLLER</p> <p>url_for(@person) = <%= @person_url %></p> <p>url_for(:id => 1, :only_path => false) = <%= @person_url_only_path_false %></p> <p>url_for(:id => 1, :only_path => true) = <%= @person_url_only_path_true %></p>

    The documentation is correct.

    June 22nd, 2009 @ 05:01 PM

  4. Andrew Vit
    Andrew Vit

    IMHO, it would read better if the option were named :full_url, :full_path, or :fully_qualified.

    It's always clearer if the option is stated as a positive, so the default can be false. (For that matter, why isn't there a path_for method like with the other url helper methods?)

    January 1st, 2010 @ 01:15 AM

  5. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.x to 3.x

    May 4th, 2010 @ 06:48 PM

  6. Santiago Pastorino
    Santiago Pastorino
    • State changed from new to open
    • Importance changed from to

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

    February 2nd, 2011 @ 04:39 PM

  7. Santiago Pastorino
    Santiago Pastorino
    • State changed from open to stale

    February 2nd, 2011 @ 04:39 PM