I am trying to implement doorkeeper into my Rails App. My App consists of 2 mountable engines. Api and CoreApi.
constraints :subdomain => 'api' do
mount Api::Engine => '/'
end
constraints :subdomain => 'core.api' do
mount CoreApi::Engine => '/'
end
I want to use Doorkeeper in the CoreApi Engine to protect those endpoints with OAuth. I have included the Doorkeeper gem in my gemspec for that engine. I have also set the symbol in the initializer.
CoreApi::Doorkeeper = ::Doorkeeper
Everything is working except when I try to visit the applications index path for doorkeeper. I ran rake routes and the paths for the CoreApi engine list all the doorkeeper paths. The issue is
undefined local variable or method `new_oauth_application_path'
its referring to the url helper on line 11 of doorkeeper-1.0.0/app/views/doorkeeper/applications/index.html.erb
I can't figure out why this is not resolving. I assume its because doorkeeper is a rails mountable engine and I am trying to mount it inside a rails mountable engine.
Add to first line of config/initializers/doorkeeper.rb:
Doorkeeper::ApplicationController.send(:include, CoreApi::Engine.routes.url_helpers)
Related
I'm working on a Sinatra app for the first time and am stumbling into an issue with serving a javascript asset.
I am following the Sinatra convention of putting the static files in “public” folder and it works locally but when we create a Docker image, we get a 404 while this works with localhost.
I see where I can set the public_folder which I have tried like this:
http://sinatrarb.com/configuration.html
but still 404'ing. Is there a way to get the top-level object and ask it where it expects the public_folder to be (like Rails.env)?
You can check the settings object.
irb(main):001:0> require "sinatra"
=> true
irb(main):002:0> settings.public_folder
=> "/usr/lib/ruby/2.5.0/irb/public"
This allows you to create a route which returns the path, something like this
require 'sinatra'
get '/' do
settings.public_folder
end
Without more information I would guess that your public folder points to a wrong directory inside your docker because the project :root points to a different directory that what you expect.
I am looking at a gist showing me how to handle CORS request in a Rails app. My routes.rb needs to have this bit of code in it.
Rails.application.routes.draw do
controller :whatever, path: '/whatever' do
match 'post_action', via: [ :post, :options]
end
end
If I was looking at a post resource, could someone show me how to set this up? I mainly do not understand the match function.
It will match the /whatever/post_action with the Controller/Action whatever#post_action. One useful tool you could use to see what your routes.rb are doing is to run the command:
rake routes
That will display all the routing table of your Rails app.
You can find more info in the Rails guide http://guides.rubyonrails.org/routing.html
Take care!
Actually match is not the good way to handle routes.
In rails 4 also it removed.
You can use the following code to use it:-
resource :whatever do
post 'post_action'
end
To test your routes
rake routes
I am using sessions in my Facebook Canvas Application using Sinatra and Rack.
The error that occurs in Facebook Canvas is:
NoMethodError - undefined method `session' for #<Hash:0xa3ed0a0>:
/home/apoorv/.rvm/gems/ruby-1.9.2-p320/gems/sinatra-1.3.2/lib/sinatra/base.rb:170:in `session'
The problem is surely with Rack because when I run my application as follows:
ruby application.rb -p 3000
it does not display any error. I have tried installing rack version: 1.3.6 and 1.4.1, but the error persists.
I have also tried using the following code instead of enable :sessions
use Rack::Session::Cookie, :key => 'rack.session',
:domain => 'static.ak.facebook.com',
:path => '/',
:secret => 'change_me'
Do I need to upgrade/degrade to lower version of Rack or add some piece of Code to make this thing work?
edited
Also before coming across this issue I had an issue integrating my application in Facebook Canvas which was resolved by adding this line in config.ru:
set :protection, :except => [:remote_token, :frame_options]
Using env['rack.session'] instead of session[] in the POST request recieved from Facebook solved this issue. Hope this helps to solve such problems
Though it has not fully resolved complications because, now adding a redirect to code in the same POST callback displays: undefined method secure? error.
I have not been able to solve this issue, though what I did was to avoid the redirect thing and instead add a functionality to the controller to handle different requests.
I have a question regarding using rack-mount with Sinatra. I've got two classic-style Sinatra apps. Let's call one App defined in app.rb and the other API defined in api.rb.
I would like it so that api.rb handles all routes beginning with '/api' and app.rb handles all other requests including the root ('/').
How would I set this up with rack-mount? Or is there a better solution than that?
I think you'll prefer Rack::URLMap - it will probably look something like this:
run Rack::URLMap.new("/" => App.new,
"/api" => Api.new)
That should go in your config.ru file.
I had a similar issue and I am not very familiar with Rack. I could not figure out what to do based on the answers above. My final solution was to have the following in config.ru.
This works perfectly for me.
# Main Ramaze site
map "/" do
Encoding.default_external = Encoding::UTF_8
Encoding.default_internal = Encoding::UTF_8
require ::File.expand_path('../app', __FILE__)
Ramaze.start(:root => __DIR__, :started => true)
run Ramaze
end
# Sinatra & Grape API
map "/api" do
use Rack::Static, :urls => ["/stylesheets", "/images", "/javascripts"], :root => "public"
use Rack::Session::Cookie
run Rack::Cascade.new([
MySinatraApp::Application,
MySinatraApp::API])
end
In config.ru you could also take advantage of Sinatra's middleware feature. If you have several Sinatra apps, each with its own routes, and want to run them simultaneously, you can arrange them in the order you want them found, e.g.
# config.ru
...
use MyAppA
use MyAppB
use MyAppC
run MyAppD
I had the same problem once and so I came up with this template: sinatra-rspec-bundler-template which is layed out for multiple apps.
It may have more features than you need but it should help you when you need something "a bit more" complex.
Does anyone had installed Devise gem with Sinatra?
Devise is based on Warden and so it should work on Sinatra, I couldn't find any related info about how to implement it.
Devise is really just a Rails-centric wrapper with nice helpers for warden, which is the underlying Rack authentication framework. So if you're using Sinatra in conjunction with Rails, you can use Devise in your Rails app, and use warden directly in your Sinatra app, and they will see the same user session data.
So no, you can't use Devise directly within your Sinatra app, but if you're building a modular app with some pieces in Rails, and other pieces in Sinatra, you can use Devise/Warden among the components.
Devise is designed for Rails only. You can't use it with Sinatra.
You can check out:
https://github.com/maxjustus/sinatra-authentication
http://www.gittr.com/index.php/archive/sinatra-basic-authentication-selectively-applied/
https://gist.github.com/243611
There is also https://github.com/jsmestad/sinatra_warden available.
I was able to get it working. There were a few main aspects:
Get Devise working with Rails (Devise is a Rails app, won't work without it)
Setup the mapping (route) on Rack level to support both Rails and Sinatra
Share the sessions between Rails and Sinatra
Setup Warden and make it available to Sinatra
Here is most relevant part of code from /config.ru:
#
# ...
# Rest with Rails
map "/" do
run MyApp::Application
end
# Anything urls starting with /slim will go to Sinatra
map "/slim" do
# make sure :key and :secret be in-sync with initializers/secret_store.rb initializers/secret_token.rb
use Rack::Session::Cookie, :key => '<< see, initializers/secret_store.rb >>', :secret => '<< copy from initializers/secret_token.rb >>'
# Point Warden to the Sinatra App
use Warden::Manager do |manager|
manager.failure_app = AppMain
manager.default_scope = Devise.default_scope
end
# Borrowed from https://gist.github.com/217362
Warden::Manager.before_failure do |env, opts|
env['REQUEST_METHOD'] = "POST"
end
run AppMain
end
See, http://labnote.beedesk.com/sinatra-warden-rails-devise for a complete solution.