3

I've got a problem similar to the one in default_url_options and rails 3. I'm attempting to implement the solution described there in the top answer, namely:

class ApplicationController < ActionController::Base
  def url_options
    { :profile => current_profile }.merge(super)
  end
end

In routes.rb (names have been changed to protect my NDA):

scope ":profile" do
  match "/:comment" => "comments#show", :as => :show_comment
  match "/comments(/*comments)" => "comments#index", :as => :show_many_comments
end

Now here's the issue I'm running into with url helpers.

show_comment_path(comment) #=> "/current_profile/comment"

That works as expected. However, it breaks on routes with optional segments.

show_many_comments_path([comment1, comment2])

yields

No route matches {:profile=>[comment1, comment2], :controller=>"comments", :action=> "index"}

However, it only breaks when using positional arguments rather than named arguments. In other words:

show_many_comments_path(:comments => [comment1, comment2]) #=> "/current_profile/comments/comment1/comment2"

as expected. So the only error case is using positional arguments and optional path segments.

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?

Edit: I'm no longer sure it's the optional 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.

Community
  • 1
  • 1
gregates
  • 6,607
  • 1
  • 31
  • 31
  • Can you give specific version of rails you're using for this? I've tried your example on 3.2.6 and it worked for me. – Serge Balyuk Aug 23 '12 at 14:19
  • v. 3.0.11. It's quite possible that it's not Rails that's at fault, but some gem or existing code that I'm overlooking (I'm currently in the process of removing subdomain-fu and reworking our routes to be subdomainless). But that itself would be useful information. – gregates Aug 23 '12 at 14:29
  • 1
    I'm seeing the exact error you describe with 3.0.11. The good news is latest versions seem to be free from it. Works as expected on 3.2.6 and 3.2.2 – Serge Balyuk Aug 23 '12 at 15:16
  • @SergeBalyuk - If you're game to recap the solution as your own answer, I'll delete my answer. (See http://meta.stackexchange.com/questions/90263/unanswered-question-answered-in-comments for elaboration of why this is helpful.) Thanks! – DreadPirateShawn Oct 09 '13 at 16:25

1 Answers1

0

Copying the answer from the comments in order to remove this question from the "Unanswered" filter:

I'm seeing the exact error you describe with 3.0.11. The good news is latest versions seem to be free from it. Works as expected on 3.2.6 and 3.2.2

~ answer per Serge Balyuk

Community
  • 1
  • 1
DreadPirateShawn
  • 8,164
  • 4
  • 49
  • 71