Undefined method `respond_to' for Api::V1::Controller - ruby-on-rails

I already went through the topics here but the solutions mentioned didn't work out for me.
I want to create an API for my rails application and followed the tutorial from Railscast for API versioning. I am using Rails 5.
This is my routes.rb file:
Rails.application.routes.draw do
namespace :api, defaults: { format: 'json' } do
namespace :v1 do
resources :orders
end
end
end
And this the neworders_controller.rb under controllers/api/v1:
module Api
module V1
class OrdersController < ApplicationController
respond_to :json
def index
respond_with Order.all
end
end
end
end
When visiting http://localhost:3000/api/v1/orders I always get the following exception:
undefined method `respond_to' for Api::V1::OrdersController:Class Did you mean? respond_to?
I have already added responder to my gemfile but that didn't work out.

In your orders controller, you made a spelling mistake. Change
repsond_with Order.all
to
respond_with Order.all
Also, you made a spelling mistake in your routes file too. Change:
namespace :api, defaults: { format: 'josn' } do
to
namespace :api, defaults: { format: 'json' } do

Related

How to setup routes in subfolder? rails

I'm really struggling how to setup controller which are placed in subfolder? I already tried below but I got an error in my console. Somebody helped how to achieve and fix this, I'm from laravel and I using rails now.
Error in my console:
'Api/Auth/register' is not a supported controller name. This can lead to potential routing problems.
My target route is this below:
http://localhost:3000/api/auth/register
Below image is directory where I place the register_controller
Here's inside of my routes.rb
Rails.application.routes.draw do
namespace 'Api' do
namespace 'Auth' do
get 'register', to: 'register#store'
end
end
end
And in my register_controller.rb is below:
module Api
module Auth
class RegisterController < ApplicationController
def store
render json: { code: 200, data: 'sample' }, status: :ok
end
end
end
end
You need to setup a namespace for your controller, and then a route for each of your actions (methods in your controllers). Also, the namespace is usually lower case.
Rails.application.routes.draw do
namespace 'api' do
namespace 'auth' do
namespace 'register' do
get 'store'
end
end
end
end

dynamic urls in rails params

I am practicing to make API in rails. The api has currently only one end point which receives a url via get request. My routes are:
Rails.application.routes.draw do
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
namespace :api, defaults: { format: :json } do
namespace :v1 do # resources :orders
get "*link" => "application#parse_link"
end
end
end
My Application Controller Code:
require 'open-uri'
class Api::V1::ApplicationController < ActionController::Base
respond_to :json
# protect_from_forgery with: :exception
def parse_link
begin
url = URI.parse(params[:link])
doc = Nokogiri::HTML(open(url).read)
rescue
redirect_to 'http://localhost:3000/'
end
end
end
When I send urls like this:
http://localhost:3000/api/v1/http://stackoverflow.com/questions/41138538/dynamic-urls-in-rails-params
It works fine
But the following type of urls does not work:
http://localhost:3000/api/v1/http://stackoverflow.com/
In this case, it splits the link and give me these params
<ActionController::Parameters {"link"=>"http:/stackoverflow", "format"=>"com"} permitted: true>
As you can see it splits the given url and save half of it in link param and the ".com" part in format params.
Thanks
Try changing defaults to constraints:
Rails.application.routes.draw do
namespace :api, constraints: { format: :json } do
namespace :v1 do # resources :orders
get "*link" => "application#parse_link"
end
end
end
Notice: it will constraint format to be json, not only set it as default. Rails treated .com at end of your URL as response type.

Changing method of parameter specification in rails url

In my API, I've implemented the following way of showing a users account in JSON (very simple).
class API::V1::UsersController < ApplicationController
respond_to :json
def show
respond_with User.find(params[:id])
end
end
This is my routes.rb
Rails.application.routes.draw do
devise_for :users
# Api definition
namespace :api, defaults: { format: :json } do
namespace :v1 do
resources :users, :only => [:show]
end
end
end
As of now, this works by allowing my to browse to the URL: http://localhost/api/v1/users/1 to show the user account with ID 1. What I want is to be able to type http://localhost/api/v1/users/show?id=1 to allow for the possibility of specifying more than just the one parameter to the show method.
I've setup a rails application that expects the parameters to be specified in this way in the past but this time around it's not working. I'm assuming it's something to do with the way I've defined the route in my routes.rb (first time I'm using the resource do notation). Any help would be greatly appreciated. thanks!
Figured it out.
Routes.rb:
Rails.application.routes.draw do
devise_for :users
# Api definition
namespace :api, defaults: { format: :json } do
namespace :v1 do
#resources :users, :only => [:show]
get '/users/show/' => 'users#show'
end
end
end

Creating custom JSON response with Jbuilder

I am attempting to create a custom JSON response in Jbuilder. The scheme is pretty simple:
json.name = #locations.name
json.description = #locations.description
However, I am getting a Missing Template error because I have namespaced my route and cannot use the current index.son.jbuilder. Currently, my route is setup as
Rails.application.routes.draw do
namespace :api, defaults: {format: 'json'} do
namespace :v1 do
resources :locations
end
end
root 'locations#index'
resources :locations
...
And my controller is pretty simple:
module Api
module V1
class LocationsController < ApplicationController
respond_to :json
def index
#locations = Location.all
end
end
end
end
It does not currently use my index.son.jbuilder. I have attempted to create a new json file with matches the naming scheme api.v1.index.json.jbuilder but the error persists. What am I missing?

Ruby on Rails route to controller not working

Attempting to create an API using doorkeeper. When I sign into my account and the session user is authenticated and access my API on
http://localhost:3000/api/v1/trials
I get a routing error page saying "uninitialized constant TrialsController" and a rake routes listing. Including:
trials_path GET /api/v1/trials(.:format) trials#index
I am using rails version 4.0.3, ruby 2.0.0. This is my config/routes.rb file:
MyApp::Application.routes.draw do
devise_for :users
use_doorkeeper :scope => 'oauth2' do
end
root :to => 'welcome#index'
scope 'api' do
scope 'v1' do
resources :trials
end
end
end
My app/ dir contains:
app/
controllers/
application_controller.rb
welcome_controller.rb
api/
v1/
trials_controller.rb
My trials_controller.rb is:
module Api::V1
class TrialsController < ::ApplicationController
doorkeeper_for :all
def index
#trials = Trials.all
end
def show
...
end
...
end
end
UPDATE:
When I change the routes.rb to namespace the trails controller like so:
namespace :api do
namespace :v1 do
resources :trails
end
end
I get a "no route matches" error when attempting to access:
http://localhost:3000/api/v1/trials(.json)
(With or without the extension.)
I have also added to trials#index:
def index
#trials = Trials.all
respond_to do |format|
format.json { render :json => #trials }
format.xml { render :xml => #trials }
end
end
Also with no luck.
I'm not sure how it would play out against doorkeeper but I have a similar API structure in an app. I have the following in my routes (matching what Sergio notes)
namespace :api, defaults: {format: 'json'} do
namespace :v1 do
resources :api_controller_1
resources :api_controller_2
end
end
And here's how I build my API classes:
module Api
module V1
class ApiNamedController < ApplicationController
# code
end
end
end

Resources