A proof of concept of having Sinatra like routes inside your controllers.
Since the router is gone, feel free to remove config/routes.rb
.
Then add the file below to lib/action_controller/inline_routes.rb
inside your app.
Then, all you need to do is to tell your Rails application what is the endpoint it should use. It is as easy as:
class Application < Rails::Application
endpoint lambda { |env| ApplicationController.call(env) }
end
We use a lambda
so the controller is reloaded in development.
You could pass simply endpoint ApplicationController
if you
don't care about reloading.
Now, Rails will skip the router and go direct to ApplicationController
.
In there, simply do:
require "action_controller/inline_routes"
class ApplicationController < ActionController::Base
include ActionController::InlineRoutes
protect_from_forgery
get "/hello" do
render text: "Hello Gorgeous!"
end
end
And that is it! You can use all Rails helpers, filters, views, etc.
Internally, ActionController::InlineRoutes
is going to
define an index action for you. So callbacks, middleware
and everything works just fine and as if you were inside
the index action.
In cases no route matches, the method not_found
can be called, which you can then further customize
or simply do nothing and let Rails default behaviour
kick in.
You could even mix Rails conventional actions with this Sinatra routes style, but please don't!
-
Sinatra routes pattern matching does not work. You can't do "/posts/:id" yet, if you need this, fork it!
-
Add support to
mount
(like in Rails routers, also calledforward
in Sinatra(. This will allow you to organize your code in more than one controller. Again, fork it if you need it! -
Make it a gem with proper README, release and docs!
Copyright (c) 2012 José Valim
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@josevalim I've pushed a simple gem for that yesterday. Supports pattern matching!