How to setup api controller without the API version of Rails - ruby-on-rails

I'm using Rails 5 to create an API but can not use the Rail 5 API only mode as I need Devise for user auth. Here's is how I have my first API controller setup:
routes.rb
Rails.application.routes.draw do
namespace :api do
namespace :v1 do
resources :skills
end
end
get '/*path' => 'home#index'
end
/controllers/api_controller.rb
class ApiController < ActionController::API
end
/controllers/api/v1/skills_controller.rb
class SkillsController < ApiController
def index
#skills = Skill.all
json_response(#todos)
end
def json_response(object, status = :ok)
render json: object, status: status
end
end
When I go to test this in the browser like so: http://localhost:4300/api/v1/skills.json
I'm getting the following errors in the rails log:
Started GET "/api/v1/skills.json" for 127.0.0.1 at 2017-05-25 08:44:12 -0700
TypeError (superclass mismatch for class SkillsController):
app/controllers/api/v1/skills_controller.rb:1:in `<top (required)>'
Error during failsafe response: superclass mismatch for class SkillsController
ActionView::MissingTemplate (Missing template public/index.html with {:locale=>[:en], :formats=>[:html, :text, :js, :css, :ics, :csv, :vcf, :png, :jpeg, :gif, :bmp, :tiff, :svg, :mpeg, :xml, :rss, :atom, :yaml, :multipart_form, :url_encoded_form, :json, :pdf, :zip, :gzip], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby]}. Searched in:
How can I set this up to work?

Set up your controller like this:
# /controllers/api/v1/skills_controller.rb
class Api::V1::SkillsController < ApplicationController
# fill in the rest
end

Related

Ruby On Rails - Pass parameters from front end to database through controllers

How to pass parameters from front end to back-end API in Ruby on Rails only through controllers? I do not want to use model or views for this.
I am using a Ruby Gem which captures some usage data which needs to be stored into the back end database.
I have created a controller, to which the post parameters are sent, but I get an error saying view is not found.
ActionView::MissingTemplate (Missing template usage_metrics/create, application/create with {:locale=>[:en], :formats=>[:json, :js, :html, :text, :js, :css, :ics, :csv, :vcf, :png, :jpeg, :gif, :bmp, :tiff, :mpeg, :xml, :rss, :atom, :yaml, :multipart_form, :url_encoded_form, :json, :pdf, :zip, :web_console_v2], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder]}. Searched in:
* "/home/local/www/cc2/cc_user-frontend/app/views"
* "/home/local/.rvm/gems/ruby-2.2.3/gems/ckeditor-4.2.2/app/views"
):
You need to send the request in an appropriate format (i.e. json) and ensure the controller action responds to this. At the moment it's trying to respond with html but not finding a template for this.
For example, in the action, after you've done what you need to do:
respond_to do |format|
format.json { render json: { whatever: is_needed_in_the_response } }
end
And you'll get your response that way.
Or if you don't need a response after parsing the params, you can use render nothing: true.
N.B. I think the newest versions Rails will respond with nothing / json if there's no template found. Not sure how this affects you.
Hope that helps!
if you want to return some data as json you can use
render json: data
but if you only want to return status
render json: {}, status: 200

Rails Template is missing render json

actually i use rails for my REST API, and i need transform my object to json but when i try i got this error:
<h1>Template is missing</h1>
<p>Missing template firms/show, application/show with {:locale=>[:en], :formats=>[:html, :text, :js, :css, :ics, :csv, :png, :jpeg, :gif, :bmp, :tiff, :mpeg, :xml, :rss, :atom, :yaml, :multipart_form, :url_encoded_form, :json, :pdf, :zip], :handlers=>[:erb, :builder, :arb, :jbuilder]}. Searched in:
* "/Users/allan/Desktop/Work/back/app/views"
* "/Library/Ruby/Gems/2.0.0/gems/activeadmin-0.6.0/app/views"
* "/Library/Ruby/Gems/2.0.0/gems/kaminari-0.16.3/app/views"
* "/Library/Ruby/Gems/2.0.0/gems/devise_invitable-1.5.5/app/views"
* "/Library/Ruby/Gems/2.0.0/gems/devise-3.5.4/app/views"
</p>
This is my code
def show
firm= Firm.find_by_subdomain(params[:subdomain])
if firm.present?
respond_to do |format|
#firm = firm
format.json { render :json => #firm.to_json }
end
end
end
I hope someone here can help me :)
Solve:
def show
render json: Firm.find_by_subdomain(current_subdomain)
end
thank you
Template missing means that you asking for a html view, not doing a json request.
If you want to always return json format regardless of format param, do this:
before_action :set_default_response_format
protected
def set_default_response_format
request.format = :json
end
#source: Rails 4 - How to render JSON regardless of requested format?
Try to add .json at the end of you query when querying your route. Without this, it will try to render html and it will go search for view file that might not be present in your case.

getting ActionView::MissingTemplate

i have a method called def createCustomer so i created a .html.erb file called createCustomer within the folder name charge which is the name of the controller. This charge folder is in under the views folder.
i am getting this on my logs
ActionView::MissingTemplate (Missing template charge/createCustomer, application/createCustomer with {:locale=>[:en], :formats=>[:html, :text, :js, :css, :ics, :csv, :vcf, :png, :jpeg, :gif, :bmp, :tiff, :mpeg, :xml, :rss, :atom, :yaml, :multipart_form, :url_encoded_form, :json, :pdf, :zip], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder]}. Searched in: 2016-01-26T23:00:34.324498+00:00 app[web.1]: * "/app/app/views"
To add to the comments, you need to equate yourself with the Rails infrastructure (convention over configuration):
Class names are CamelCase, file names are snake_case
Controllers are plural (mostly)
You should keep your controllers resourceful
Here's how it should be structured:
# config/routes.rb
resources :customers #-> url.com/customers/new
# app/controllers/customers_controller.rb
class CustomersController < ApplicationController
def new
#customer = Customer.new
end
def create
#customer = Customer.new customer_params
redirect_to #customer if #customer.save
end
private
def customer_params
params.require(:customer).permit(...)
end
end
# app/views/customers/new.html.erb
<%= form_for #customer do |f| %>
...

Rails 4 - Can't render 404 in production with dynamic error page (Error during failsafe response: Missing template errors/show)

I created dynamic error pages following the instructions here:
http://wearestac.com/blog/dynamic-error-pages-in-rails
They work when I go to them directly via /404 or /500 or if I attempt to go to a URL that is not handled in the routes. However, when I attempt to force a 404 in an action with render :status => 404 in production I get the following error:
Error during failsafe response: Missing template errors/show, application/show with {:locale=>[:en, :"en-US"], :formats=>[:html, :text, :js, :css, :ics, :csv, :vcf, :png, :jpeg, :gif, :bmp, :tiff, :mpeg, :xml, :rss, :atom, :yaml, :multipart_form, :url_encoded_form, :json, :pdf, :zip], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby]}.
Searched in:
* "/Users/Jeremy/Projects/git/rails/app/views"
The 404 error page lives at views/errors/404.html.erb. From the stack trace it doesn't even look like Rails is accessing the ErrorsController. (both code and trace are below)
In development mode I get the standard rails error page, which is to be expected.
application.rb
...
config.exceptions_app = self.routes
...
routes.rb
...
# Error pages
%w( 404 500 ).each do |code|
get code, :to => "errors#show", :code => code
end
...
errors_controller.rb
class ErrorsController < ApplicationController
def show
render status_code.to_s, :status => status_code
end
protected
def status_code
params[:code] || 500
end
end
The stack trace doesn't seem very helpeful:
Error during failsafe response: Missing template errors/show, application/show with {:locale=>[:en, :"en-US"], :formats=>[:html, :text, :js, :css, :ics, :csv, :vcf, :png, :jpeg, :gif, :bmp, :tiff, :mpeg, :xml, :rss, :atom, :yaml, :multipart_form, :url_encoded_form, :json, :pdf, :zip], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby]}. Searched in:
* "/Users/Jeremy/Projects/git/rails/app/views"
/Users/Jeremy/.rvm/gems/ruby-2.1.0/gems/actionview-4.1.1/lib/action_view/path_set.rb:46:in `find'
/Users/Jeremy/.rvm/gems/ruby-2.1.0/gems/actionview-4.1.1/lib/action_view/lookup_context.rb:124:in `find'
/Users/Jeremy/.rvm/gems/ruby-2.1.0/gems/actionview-4.1.1/lib/action_view/renderer/abstract_renderer.rb:18:in `find_template'
/Users/Jeremy/.rvm/gems/ruby-2.1.0/gems/actionview-4.1.1/lib/action_view/renderer/template_renderer.rb:41:in `determine_template'
/Users/Jeremy/.rvm/gems/ruby-2.1.0/gems/actionview-4.1.1/lib/action_view/renderer/template_renderer.rb:8:in `render'
/Users/Jeremy/.rvm/gems/ruby-2.1.0/gems/actionview-4.1.1/lib/action_view/renderer/renderer.rb:42:in `render_template'
/Users/Jeremy/.rvm/gems/ruby-2.1.0/gems/actionview-4.1.1/lib/action_view/renderer/renderer.rb:23:in `render'
/Users/Jeremy/.rvm/gems/ruby-2.1.0/gems/actionview-4.1.1/lib/action_view/rendering.rb:99:in `_render_template'
/Users/Jeremy/.rvm/gems/ruby-2.1.0/gems/actionpack-4.1.1/lib/action_controller/metal/streaming.rb:217:in `_render_template'
/Users/Jeremy/.rvm/gems/ruby-2.1.0/gems/actionview-4.1.1/lib/action_view/rendering.rb:82:in `render_to_body'
/Users/Jeremy/.rvm/gems/ruby-2.1.0/gems/actionpack-4.1.1/lib/action_controller/metal/rendering.rb:32:in `render_to_body'
/Users/Jeremy/.rvm/gems/ruby-2.1.0/gems/actionpack-4.1.1/lib/action_controller/metal/renderers.rb:32:in `render_to_body'
/Users/Jeremy/.rvm/gems/ruby-2.1.0/gems/actionpack-4.1.1/lib/abstract_controller/rendering.rb:25:in `render'
/Users/Jeremy/.rvm/gems/ruby-2.1.0/gems/actionpack-4.1.1/lib/action_controller/metal/rendering.rb:16:in `render'
/Users/Jeremy/.rvm/gems/ruby-2.1.0/gems/actionpack-4.1.1/lib/action_controller/metal/instrumentation.rb:41:in `block (2 levels) in render'
/Users/Jeremy/.rvm/gems/ruby-2.1.0/gems/activesupport-4.1.1/lib/active_support/core_ext/benchmark.rb:12:in `block in ms'
/Users/Jeremy/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/benchmark.rb:294:in `realtime'
/Users/Jeremy/.rvm/gems/ruby-2.1.0/gems/activesupport-4.1.1/lib/active_support/core_ext/benchmark.rb:12:in `ms'
/Users/Jeremy/.rvm/gems/ruby-2.1.0/gems/actionpack-4.1.1/lib/action_controller/metal/instrumentation.rb:41:in `block in render'
/Users/Jeremy/.rvm/gems/ruby-2.1.0/gems/actionpack-4.1.1/lib/action_controller/metal/instrumentation.rb:84:in `cleanup_view_runtime'
/Users/Jeremy/.rvm/gems/ruby-2.1.0/gems/actionpack-4.1.1/lib/action_controller/metal/instrumentation.rb:40:in `render'
/Users/Jeremy/Projects/git/rails/app/controllers/application_controller.rb:86:in `render_not_found'
/Users/Jeremy/Projects/git/rails/app/controllers/application_controller.rb:39:in `set_catalog_type'
Try specifying a template path
def render_error(status)
render status: status, template: "errors/#{status}"
end
Try:
class ErrorsController < ApplicationController
layout "errors"
before_action :set_format
def show
render status_code.to_s, status: status_code
end
private
def status_code
params[:code]
end
def set_format
request.format = :html
end
end

Missing template action error in rails3

I have defined a method in controller that is like:
def self.dailymail
.... #fill data from db
ac = ActionController::Base.new()
kit = PDFKit.new(ac.render_to_string(:action => "formatinhtml.html.erb",:rawdata => data))
pdf = kit.to_pdf
... #send pdf in mail
end
formatinhtml is like:
def formatinhtml
#dailyrep = params[:rawdata]
respond_to do |format|
format.html # daily.html.erb
end
end
I have to use self.dailymail so that I can call it from model & in turn from rufus scheduler.But,still I get error such as:
scheduler caught exception:
Missing template action_controller/base/daily.html with {:locale=>[:en], :formats=>[:html, :text, :js, :css, :ics, :csv, :png, :jpeg, :gif, :bmp, :tiff, :mpeg, :xml, :rss, :atom, :yaml, :multipart_form, :url_encoded_form, :json, :pdf, :zip, :xls], :handlers=>[:erb, :builder, :coffee]}. Searched in:
* "F:/DEVELOPMENT/TrackIt/app/views"
C:/Ruby193/lib/ruby/gems/1.9.1/gems/actionpack-3.2.8/lib/action_view/path_set.rb:58:in `find'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/actionpack-3.2.8/lib/action_view/lookup_context.rb:109:in `find'...
so,what does I need to do?
Update: After debugging,I found action formatinhtml is not actually being getting called;I have defined neccessary routes.
Your folder where "daily.html" is located is not the same as your controller

Resources