How can I create a Rails 3 route that will match all requests and direct to one resource / page?
Rails needs to bind the url parameters to a variable, try this:
match '*foo' => 'content#holding'
If you also want to match /
, use parenthesis to specify that foo
is optional:
match '(*foo)' => 'content#holding'
Ruby-on-Rails: route for path with arbitrary number of components?
Oh! I found the answer here: Rails catch-all route
I just do this ...
get "xyz/*pathvar", to: "xyz#handler"
... and the full path that appears after /xyz
will appear in params['pathvar']
.
Redirect all break url to a specific method - In Ruby on Rails
duplicate question
Rails catch-all route
put
match '*path' => 'your_controller#your_action' at the end of the
routes.rb file. This is important, since the routes are stepped
through top down.See also http://guides.rubyonrails.org/routing.html -> 3.10
all routes that are not catched prior to this one will be dispatched to your_controller#your_action
Rails route only if resource is present
In an ideal world, I'd do something like this in my routes.rb file:
No way. In Rails
World, this functionality should go inside controller.
In controller you can do something like
def show
if Promo.exists?(promo_id)
#do something
else
raise ActionController::RoutingError.new('Not Found')
end
end
Update
With routes, you can do something like this
constraints(lambda { |req| Promo.exists?(req.params["promo_id"]) }) do
get '/:promo_id' => 'promos#show
end
Please keep in mind that this constraints will query the database for every request with a url matching the pattern /:promo_id
(e.q. /users
, /faq
). To avoid unnecessary database queries that decrease your website performance, you should add this rule as far as possible to the end of your routes.rb
.
How to override Rails 3.1 Routing Error when using engines with glob routes?
The right way to handle this used to be rescue_from
and a custom error handler, rather than with an engines-hostile catchall route. However, custom error handlers are no longer supported in Rails 3.1 and this likely won't be fixed until Rails 3.2, if ever. If you need custom error handling and you use engines with routes, the vidibus-routing_error
gem provides a workaround.
Another option is to put your custom error handler into a Rack endpoint at the bottom of your stack.
Non-Resourceful Routes and POST
The get
method in your route is declaring a single route, which in this case is a GET request. If you want to define both a custom GET and a POST in one line, you can use match
.
From the Rails Guide:
match 'photos/show' => 'photos#show', :via => [:get, :post]
Catch-all/wildcard route in Elixir's Phoenix?
Ah, the famous pokemon route:
get "/*path"
You will find the paths inside conn.params["path"]
or as conn.path_info
.
Related Topics
Is the Server Running on Host "Localhost" (::1) and Accepting Tcp/Ip Connections on Port 5432
Differencebetween Raising Exceptions VS Throwing Exceptions in Ruby
Rails Catch-All/Globbing Routes
Rails Attachments Inline Are Not Shown Correctly in Gmail
Is the &Method(:Method_Name) Idiom Bad for Performance in Ruby
Is Order of a Ruby Hash Literal Guaranteed
Is There a Hook Similar to Class#Inherited That's Triggered Only After a Ruby Class Definition
Passing Block into a Method - Ruby
How to Install Ruby-Debug When Needing Necessary Libraries And/Or Headers
Uninstall All Installed Gems, in Osx
How to Split a String to Get All the Substrings by Ruby
How to Find a Hash Key Containing a Matching Value
Rails 2 to Rails 3:Using Link_To Instead of Link_To_Remote (Including Remote and Update)
What Is Causing This Activerecord::Readonlyrecord Error
What Is the Purpose of "!" and "" at the End of Method Names