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.