Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Allow a route to have :format => true #2263

Merged
merged 2 commits into from

3 participants

@sferik

When :format => true, the format segment is mandatory (as opposed to :format => false).

Before this patch, it was not possible to specify a resource route with a mandatory format.

sferik added some commits
@sferik sferik Allow a route to have :format => true
When format is true, it is mandatory (as opposed to :format => false).
This is currently not possible with resource routes, which automatically
make format optional by default.
c41ecd7
@sferik sferik Add documentation for :format => true 78d151d
@bcardarella

Does this make sense for 3.1? It's already in release candiate, this should probably go to master

@sferik

Sad face. If it has to wait until 3.2, I suppose it can wait.

I guess I'd argue it's a pretty obvious missing feature (i.e. the fact that you can say :format => false but not :format => true feels like a bug). Of course, the last thing I'd want to do is hinder the 3.1 release process.

To whoever gets to make the call: Just let me know what you want me to do. I'm happy to submit another pull request that applies to master.

@bcardarella

Yeah, I hear you. It just feels dirty introducing new public api behavior in a RC cycle

@josevalim
Owner

It is fine to go into 3-1-stable. @sferik, if this is not in master yet, please reopen that other pull request and ping me.

@josevalim josevalim merged commit 19a3c9b into rails:3-1-stable
@bcardarella

sweet :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jul 25, 2011
  1. @sferik

    Allow a route to have :format => true

    sferik authored
    When format is true, it is mandatory (as opposed to :format => false).
    This is currently not possible with resource routes, which automatically
    make format optional by default.
  2. @sferik
This page is out of date. Refresh to see the latest.
View
2  actionpack/lib/action_dispatch/routing/mapper.rb
@@ -116,6 +116,8 @@ def normalize_path(path)
path
elsif path.include?(":format") || path.end_with?('/')
path
+ elsif @options[:format] == true
+ "#{path}.:format"
else
"#{path}(.:format)"
end
View
7 actionpack/test/dispatch/mapper_test.rb
@@ -83,6 +83,13 @@ def test_map_wildcard_with_format_false
assert_equal '/*path', fakeset.conditions.first[:path_info]
assert_nil fakeset.requirements.first[:path]
end
+
+ def test_map_wildcard_with_format_true
+ fakeset = FakeSet.new
+ mapper = Mapper.new fakeset
+ mapper.match '/*path', :to => 'pages#show', :format => true
+ assert_equal '/*path.:format', fakeset.conditions.first[:path_info]
+ end
end
end
end
View
6 railties/guides/source/routing.textile
@@ -569,6 +569,12 @@ NOTE: By requesting +"/foo/bar.json"+, your +params[:pages]+ will be equals to +
match '*pages' => 'pages#show', :format => false
</ruby>
+NOTE: If you want to make the format segment mandatory, so it cannot be omitted, you can supply +:format => true+ like this:
+
+<ruby>
+match '*pages' => 'pages#show', :format => true
+</ruby>
+
h4. Redirection
You can redirect any path to another path using the +redirect+ helper in your router:
Something went wrong with that request. Please try again.