rails-api

ActionController::RoutingError: uninitialized constant Api::V1::ApiController

拥有回忆 提交于 2019-12-05 10:19:14
I have Rails 5 API project for controlling user tasks and I have the following error but not always for the same controller and route. ActionController::RoutingError: uninitialized constant Api::V1::ApiController I describe you a little bit my project to explain in more detail the error. App structure Routes scope module: 'api' do namespace :v1 do # => Login routes scope module: 'login' do match 'login', to: 'sessions#login', as: 'login', via: :post end # => Team routes scope module: 'team' do # => no admin routes resources :tasks, except: [:index] do collection do match ':view', to: 'tasks

Force all received requests Content-Type to JSON

99封情书 提交于 2019-12-04 00:09:45
I'm actually working on an API which uses Rails 4. I would like to set the Content-Type of a request to JSON if the client does not specify a media type in Content-Type header. In order to get that behaviour I tried to add the following before_action in my ApplicationController : def set_request_default_content_type request.format = :json end In my RegistrationsController#create method I have a breakpoint to check if everything is working. Well, the request.format trick does not work, despite the value is set to application/json it seems that the controller (or Rails internals) do not consider

Using rails_admin with rails_api

╄→гoц情女王★ 提交于 2019-12-03 12:41:10
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:

How do you convert a Rails 5 API app to a rails app that can act as both API and app?

那年仲夏 提交于 2019-12-03 11:16:57
问题 I initially created it in rails 5 with the --api tag. From http://edgeguides.rubyonrails.org/api_app.html, I removed config.api_only = true I changed class ApplicationController < ActionController::API end to class ApplicationController < ActionController::Base end The problem I'm having now is when the view is getting rendered, ex. welcome/index.html.erb , the corresponding CSS file assets/stylesheets/welcome.css.scss is not. Any idea how I can fix this, or more generally convert the API

Controller Specs vs Request Specs?

一个人想着一个人 提交于 2019-12-03 10:35:28
问题 I'm working on a rails API and I'm now planning on writing some RSpec tests for the controllers. I've been reading around and I haven't been able to figure out what the actual difference between controller specs and request specs are and which one I should probably use if I'm testing an API. 回答1: Rails 3 & 4 Controller specs - A controller spec is an RSpec wrapper for a Rails functional test. It allows you to simulate a single http request in each example, and then specify expected outcomes

Rails: Restrict API requests to JSON format

放肆的年华 提交于 2019-12-03 08:24:25
问题 I would like to restrict requests to all API controllers to being redirected to the JSON path. I would like to use a redirect since also the URL should change according to the response. One option would be to use a before_filter which redirects the request to the same action but forces the JSON format. The example is not working yet! # base_controller.rb class Api::V1::BaseController < InheritedResources::Base before_filter :force_response_format respond_to :json def force_response_format

Token based authentication for Rails JSON APIs

谁都会走 提交于 2019-12-03 06:59:53
问题 I make API in rails. For normal authentication we use devise but in API how to implement devise for authentication. gem 'devise_token_auth' Someone prefer this this gem for authentication but there are no tutorial available for that. How to implement authenitication in rails api? 回答1: The best thing you can do is to follow the github tutorials which are most likely to be up-to-date. First you should follow the TLDR part. Note that the frontend developpers need to know about the usage

Rails: Restrict API requests to JSON format

三世轮回 提交于 2019-12-02 22:08:27
I would like to restrict requests to all API controllers to being redirected to the JSON path. I would like to use a redirect since also the URL should change according to the response. One option would be to use a before_filter which redirects the request to the same action but forces the JSON format. The example is not working yet! # base_controller.rb class Api::V1::BaseController < InheritedResources::Base before_filter :force_response_format respond_to :json def force_response_format redirect_to, params[:format] = :json end end Another option would be to restrict the format in the routes

Rails: ActionDispatch::Request.parameter_parsers for multipart/form-data

℡╲_俬逩灬. 提交于 2019-12-02 08:08:45
问题 In my rails API, I have added an initialiser that will change the keys of the JSON input from snake-case to underscore-separated. Like so: ActionDispatch::Request.parameter_parsers[:json] = -> (raw_post) { data = ActiveSupport::JSON.decode(raw_post) data = {:_json => data} unless data.is_a?(Hash) data.deep_transform_keys!(&:underscore) } Now, certain APIs will be passed with the header: content-type: multipart/form-data instead of application/json I want to do the same for such APIs. That is

Can I delete all migration files and start from scratch?

蓝咒 提交于 2019-12-02 06:45:20
I have a Rails-API app that I'm going to re-deploy after a long time. The app is non-production, but I am ready to deploy the production version. I want to basically delete all the migration files and start from scratch using the schema, is there any problem with this approach? Assuming I can do this, what do I need to alter the schema.rb to? Is this a common practice? Seems reasonable to me. # what do I change the version param value to? ActiveRecord::Schema.define(version: 20171129023538) Migrations are used to update your database or to rollback to previous versions. Once migrations have