I am trying to generate the documentation with grape-swagger.
On my gem file
gem 'grape-entity'
gem 'grape-swagger'
gem 'grape-swagger-entity'
gem 'grape-swagger-rails'
On my endpoint
require 'grape-swagger'
module MyModule
class Api < Grape::API
content_type :json, 'application/json'
default_format :json
format :json
mount V1::Root
add_swagger_documentation
end
end
When I am visiting http://localhost:3000/swagger_doc I am getting an error,
No route matches [GET] "/swagger_doc".
Also I don't see any doc generated.
add the prefix if you have mounted a place other than root.
for example /api/swagger_doc
Related
I am using Knock for JWT authentication for Rails 5 API.
I have this Routes file:
Rails.application.routes.draw do
namespace :api, constraints: { subdomain: 'api' }, path: '/' do
namespace :v1 do
post 'user_token' => 'user_token#create'
end
end
end
With this, I expect to be able to make a POST request to create new token like this:
POST http://api.domain.com/v1/user_token
but this request gives me the following error:
NameError: uninitialized constant API::V1::User
What I can understand is that Knock is trying to access the model User on the same namespace where the controller (user_token_controller) is. But my model are not namespaced:
class User < ApplicationRecord
has_secure_password
#...
end
My user_token_controller.rb
module API
module V1
class UserTokenController < Knock::AuthTokenController
end
end
end
What I dong wrong?
It's a bug already fixed but not yet published.
The temporary solution was using the gem from github code like:
gem 'knock', github: 'psantos10/knock', branch: 'master'
https://github.com/nsarno/knock/issues/120
I am a beginner in Grape, I want to show my api list by using swagger-ui.
I put swagger html in public/swagger, and I access localhost:3000/swagger
However, it keep showing 404 not found. I thought it's causing by Grape configuration.
Here is api.rb
#app/api/twitter/api/api.rb
require 'grape'
module Twitter
class API < Grape::API
version 'v1', using: :header, vendor: 'twitter'
format :json
prefix :api
add_swagger_documentation
resource :statuses do
desc 'Return a public timeline.'
get :public_timeline do
Status.limit(20)
end
end
end
end
You don't have to do it manually by putting swagger html somewhere.
You could use gem grape-swagger provided by Grape team, add two lines in you config.ru.
require 'grape-swagger'
module API
class Root < Grape::API
format :json
...
add_swagger_documentation
end
end
then you could access swagger doc at http://localhost:3000/swagger_doc
I originally posted this as an issue on rails_api GitHub, but am now posting it here due to inactivity.
I'm trying to use rails_admin with a Rails 5 API application. I included extra ActionController modules up to the point that I can either have a functioning rails_admin panel or working API requests. The issue seems to be that rails_admin depends on ActionView::Layouts, which after included causes issues for API requests.
Gemfile:
gem 'rails', '>= 5.0.0.beta3', '< 5.1'
...
gem 'rack-pjax', github: 'afcapel/rack-pjax'
gem 'remotipart', github: 'mshibuya/remotipart'
gem 'kaminari', github: 'amatsuda/kaminari', branch: '0-17-stable'
gem 'rails_admin', github: 'sferik/rails_admin'
I configured my application to use ActionDispatch::Flash:
module MyApp
class Application < Rails::Application
...
config.middleware.use ActionDispatch::Flash
end
end
I configured extra modules for Rails API, ApplicationController:
class ApplicationController < ActionController::API
include Knock::Authenticatable
include Pundit
# RailsAdmin support
include AbstractController::Helpers
include ActionController::Flash
include ActionController::RequestForgeryProtection
include ActionController::MimeResponds
include ActionController::HttpAuthentication::Basic::ControllerMethods
include ActionView::Layouts
end
With these changes the Rails Admin dashboard seems to run fine. However, when I'm trying to access the JSON resources in my application, the following error is thrown:
Error:
BookingsControllerTest#test_should_get_index:
ActionView::MissingTemplate: Missing template bookings/index, application/index with {:locale=>[:en], :formats=>[:json], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :haml]}. Searched in:
* "/Users/richard/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/bundler/gems/rails_admin-355dc80f8a20/app/views"
The test code (also tried adding format: :json):
class BookingsControllerTest < ActionController::TestCase
test 'should get index' do
get :index
assert_response :success
end
end
This is the controller code:
class BookingsController < ApplicationController
def index
#bookings = find_bookings
render json: #bookings, include: ['customer', 'client'], meta: meta
end
end
This only happens after I include the ActionView::Layouts module in the top level ActionController::API class to support Rails Admin.
If I were you, I try isolate API and RailsAdmin controllers. I think this must work:
class ApplicationController < ActionController::API
include Knock::Authenticatable
include Pundit
end
class RailsAdminCustomController < ApplicationController
# RailsAdmin support
include AbstractController::Helpers
include ActionController::Flash
include ActionController::RequestForgeryProtection
include ActionController::MimeResponds
include ActionController::HttpAuthentication::Basic::ControllerMethods
include ActionView::Layouts
end
In config/initializers/rails_admin.rb
RailsAdmin.config do |config|
# other config stuff ...
config.parent_controller = '::RailsAdminCustomController'
end
Just check the RailsAdmin::ApplicationController here, and the config settings here.
As of v1.0.0 (released September 2016), Rails Admin now supports Rails 5 API-mode straight out-of-the-box. The gem itself injects the missing middleware to render its views and there is no extra configuration required.
Links:
Using Rails Admin with Rails 5 API application
CHANGELOG
You should have a json view file in this location bookings/index.json.jbuilder
And inside this file something like
bookings/index.json.jbuilder
json.name #bookings.name
json.date #bookings.date
This is another template missing
application/index
but really don't know you app completely. So maybe that's the application layout you implemented using ActionView::Layouts. In that case is asking you to implement a layout page file in the location application/index.
NOTE: Those two file inside the views folder.
I want to use Jbuilder with Rails 5.0.0.beta1.1 in API mode. Out of the box, it doesn't work, even when creating the app/views directory.
For example, I have:
# app/controllers/tests_controller.rb
class TestsController < ApplicationController
# The requests gets inside the action
def test
end
end
# app/views/tests/test.json.jbuilder
json.test "It works!"
The error I'm getting is
No template found for TestsController#test, rendering head :no_content
I guess I have to change some things in the config files. What do I have to do?
Doing an explicit render from the controller like this works:
render 'controller_name/action.json.jbuilder'
With API mode.
You need include module like bellow
class ApplicationController < ActionController::API
include ActionController::ImplicitRender # if you need render .jbuilder
include ActionView::Layouts # if you need layout for .jbuilder
end
I got the same error, but in my case I had simply forgotten to add the jbuilder gem in the Gemfile:
gem 'jbuilder', '~> 2.5'
Following this tutorial, I created the API of my existing blog web application on Rails. I am getting the error:
uninitialized constant API
This is my code:
lib/api/v1/articles.rb:
module API
module V1
class Articles < Grape::API
version 'v1'
format :json
resource :articles do
desc "Return list of recent posts"
get do
Article.recent.all
end
end
end
end
end
lib/api/v1/root/rb
module API
module V1
class Root < Grape::API
mount API::V1::Articles
end
end
end
lib/api/root.rb
module API
class Root < Grape::API
prefix 'api'
mount API::V1::Root
end
end
lib/tasks/routes.rake
namespace :api do
desc "API Routes"
task :routes => :environment do
API::Root.routes.each do |api|
method = api.route_method.ljust(10)
path = api.route_path.gsub(":version", api.route_version)
puts " #{method} #{path}"
end
end
end
config/routes.rb
Rails.application.routes.draw do
mount API::Root => '/'
get 'welcome/index'
root 'welcome#index'
resources :articles
end
This is the existing web application code:
app/controllers/articles_controller.rb
class ArticlesController < ApplicationController
def new
end
def create
# render plain: params[:article].inspect
#article = Article.new(article_params)
#article.save
redirect_to #article
end
def show
#article = Article.find(params[:id])
end
private
def article_params
params.require(:article).permit(:title,:text)
end
end
Now I'm in the Accessing API routes part of this article. When I run rake routes it gives the error, uninitialized constant API.. What I'm doing wrong.
Edit: As per the comment, giving the detailed error
rake aborted!
NameError: Uninitialized constant API
F:/blog/config/routes.rb:2:in 'block in <top (required)>'
F:/blog/config/routes.rb:1:in '<top (required)>'
C:in 'execute_if_updated'
F:/blog/config/environment.rb:5:in '<top (required)>'
Tasks: TOP =>routes =>environment
Contents of environment.rb
# Load the Rails application.
require File.expand_path('../application', __FILE__)
# Initialize the Rails application.
Rails.application.initialize!
When I insert config.autoload_paths += Dir["#{config.root}/lib/**/"] in your config/application.rb, and run rake routes, it gives the error can't convert Symbol into String
Upgrading the grape version, I got this error:
As per the discussion, you need to update your grape gem's version to 0.9.0 and then you need to add this line in your Gemfile:
gem 'grape', '0.9.0'
and then:
$ bundle install
As of version 0.11.0, the documentation says
Rails
Place API files into app/api. Rails expects a subdirectory that
matches the name of the Ruby module and a file name that matches the
name of the class. In our example, the file name location and
directory for Twitter::API should be app/api/twitter/api.rb.
Simple solution would be creating another api folder and moving entire files & dirs into app/api/api folder