Questions tagged [rails-api]

Rails for API only applications (>3.2)

Rails::API is a subset of a normal Rails application, created for applications that don't require all functionality that a complete Rails application provides. It is a bit more lightweight, and consequently a bit faster than a normal Rails application. The main example for its usage is in API applications only, where you usually don't need the entire Rails middleware stack nor template generation.

Using Rails for API-only Apps

This is a quick walk-through to help you get up and running with Rails::API to create API-only Apps, covering:

  • What Rails::API provides for API-only applications
  • How to decide which middlewares you will want to include
  • How to decide which modules to use in your controller
753 questions
16
votes
6 answers

Force all received requests Content-Type to JSON

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…
Mich
  • 729
  • 1
  • 7
  • 14
15
votes
4 answers

How can I get url of my attachment stored in active storage in my rails controller

How can I get url of my has_one model attachment stored in active storage in my rails controller. So, that I would be able to send it as full link as api in json. So far, I have tried following methods but each of them are giving various…
15
votes
5 answers

Allow CORS in Ruby on Rails

In my config/application.rb file, I have this code, config.action_dispatch.default_headers = { 'Access-Control-Allow-Origin' => '*', 'Access-Control-Request-Method' => 'GET, PATCH, PUT, POST, OPTIONS, DELETE' } But that does not…
Ben Aubin
  • 5,542
  • 2
  • 34
  • 54
15
votes
1 answer

How to respond to OPTIONS HTTP Method in rails-api?

I'm using rails-api to build a public json api. I would like to respond to OPTIONS HTTP Method to take advantages of Cross-Origin Resource Sharing. http://www.w3.org/TR/cors/ I'm doing this: headers['Access-Control-Allow-Methods'] = 'POST, PUT,…
yokomizor
  • 1,527
  • 1
  • 19
  • 20
14
votes
7 answers

Setting Content-Type header for RSpec and Rails-API

I'm using the rails-api gem to build a web service and want to test my API with RSpec. Every request I make, regardless of the HTTP method has the CONTENT_TYPE header set as "application/x-www-form-urlencoded". This isn't really a problem until I…
Peter Brown
  • 50,956
  • 18
  • 113
  • 146
13
votes
4 answers

How to return JSON from Rails with camelcased key names

I'm building a JS app with a Rails backend and in order not to confuse snake and camel cases, I want to normalize it all by returning camelcase key names from the server. So user.last_name would return user.lastName when returned from the API. How…
Zack Shapiro
  • 6,648
  • 17
  • 83
  • 151
13
votes
3 answers

What's the difference between using render instead of respond_with/to in a Rails API?

I am building a simple rails tutorial on how to build APIs for some students and I am building it without the respond_to and respond_with because I just want to see if I can build an api without using a gem. This is what I have and my tests…
Jwan622
  • 11,015
  • 21
  • 88
  • 181
12
votes
2 answers

REST API versioning - why aren't models versioned

I've been reading up on all the approaches to version REST APIs. In almost all implementations, controllers and views are versioned, however models are not. To give the rails example, controllers are organized as: #…
11
votes
1 answer

devise_token_auth with multiple models and auth headers

This is my problem, I override the controllers for an User model: mount_devise_token_auth_for 'User', at: 'auth', controllers: { registrations: 'v1/authentication/registrations' sessions: 'v1/authentication/sessions' …
Armando
  • 940
  • 7
  • 16
11
votes
3 answers

Why does Rails RSpec response show 302 instead of 401?

I have been stuck on this problem for several days and I don't know what is wrong with it. I started Ruby on Rails few months ago and I am currently learning authentication with API. I have looked at other similar topics here and there but none of…
Iggy
  • 5,129
  • 12
  • 53
  • 87
11
votes
2 answers

The correct way to version Rails 3 APIs

I have a Rails 3 engine which exposes API routes for around 20 controllers. Those controllers represent several different resources at various levels of nesting and are covered by over 500 rspec tests. The API is versioned at v1 using namespaces and…
Martin Foot
  • 3,594
  • 3
  • 28
  • 35
11
votes
3 answers

Using a different key name for an association attribute in rails api active model serializer

I am building a Rest API using rails-api and active-model-serializer to easily filter the required fields in the JSON. I am also using the has_one association in these serializers. All I wanted to know is how do I specify a different key name for…
swaroopsm
  • 1,389
  • 4
  • 18
  • 34
11
votes
7 answers

How to disable ActiveModel::Serializers for a specific controller?

We're using active_model_serializers - 0.8.1 in a Rails application. The app has some API specific controllers inheriting from ActionController::Metal in a way similar to rails-api's ActionController::API. Well we want to use…
Dimitris Zorbas
  • 5,187
  • 3
  • 27
  • 33
11
votes
3 answers

Session in Rails_API gem

I am using the rails_api gem in my project. I want to add session management for authentication, but it seems the session does not work. Here is my configuration in config/initializer/session_store.rb: Pmcapi::Application.config.session_store…
Ari Firmanto
  • 334
  • 1
  • 15
11
votes
4 answers

emberjs handle 401 not authorized

I am building an ember.js application and am hung up on authentication. The json rest backend is rails. Every request is authenticated using a session cookie (warden). When a user first navigates to the application root rails redirects to a login…
Aaron Renoir
  • 4,283
  • 1
  • 39
  • 61
1
2
3
50 51