Note that there are some explanatory texts on larger screens.

plurals
  1. POAdding url_options to url_for in Rails 3.0.11
    primarykey
    data
    text
    <p>I've got a problem similar to the one in <a href="https://stackoverflow.com/questions/6100954/default-url-options-and-rails-3">default_url_options and rails 3</a>. I'm attempting to implement the solution described there in the top answer, namely:</p> <pre><code>class ApplicationController &lt; ActionController::Base def url_options { :profile =&gt; current_profile }.merge(super) end end </code></pre> <p>In routes.rb (names have been changed to protect my NDA):</p> <pre><code>scope ":profile" do match "/:comment" =&gt; "comments#show", :as =&gt; :show_comment match "/comments(/*comments)" =&gt; "comments#index", :as =&gt; :show_many_comments end </code></pre> <p>Now here's the issue I'm running into with url helpers.</p> <pre><code>show_comment_path(comment) #=&gt; "/current_profile/comment" </code></pre> <p>That works as expected. However, it breaks on routes with optional segments.</p> <pre><code>show_many_comments_path([comment1, comment2]) </code></pre> <p>yields</p> <pre><code>No route matches {:profile=&gt;[comment1, comment2], :controller=&gt;"comments", :action=&gt; "index"} </code></pre> <p>However, it <em>only</em> breaks when using positional arguments rather than named arguments. In other words:</p> <pre><code>show_many_comments_path(:comments =&gt; [comment1, comment2]) #=&gt; "/current_profile/comments/comment1/comment2" </code></pre> <p>as expected. So the only error case is using positional arguments <em>and</em> optional path segments.</p> <p>I've looked through the source code in ActionDispatch, and I can't figure out why this is happening. It looks like it should be smart enough to see that we've supplied an explicit :profile, and apply the positional argument to the remaining path segment. Any insight into why it isn't doing that, or what I'd have to do to get the behavior I want?</p> <p>Edit: I'm no longer sure it's the <em>optional</em> path segments that are causing the error, as opposed to the route globbing. I thought I had previously reproduced this error on a single optional path segment, but my latest trials have optional segments behaving differently based on whether they include a * or not. In any case, I'm still curious about this, but as a practical matter I've decided to just scrap the url_options in favor of passing around explicit parameters.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload