rails-api

rails 5 API low-level caching

Deadly 提交于 2019-12-11 06:21:09
问题 I am a bit confused regarding Rails API caching. I am using JSONAPI spec and fast_jsonapi gem and trying to cache the vehicle itself on show action and if there are params coming over like include=service_notes,service_alerts then I would like to cache those too. This is my initial approach but not sure if it is right. I have 2 main issues: For the vehicle caching itself is there a better approach than my vehicle = Vehicle.find_cached(params[:id]). This is not using updated_at but an after

Merge Nested Attributes

南楼画角 提交于 2019-12-11 04:16:55
问题 I have a sample using post, comments and author models. Please see my full requirement from the following thread Nested routes and CRUD operations with additional values for assciation in Rails Here i need to create one comment when a post is created. I used nested attributes and working fine. But my issue is i don't want all attributes from the user. Some attributes needs to be added internally. I can merge if the attributes not having nested attributes. But merge not worked for nested

Devise + Rails-API

我怕爱的太早我们不能终老 提交于 2019-12-10 15:59:53
问题 So, I'm starting an API from a new rails-api project. I would like to use Devise for all the authentication stuff. I already learned a lot from my recent googling-sessions. I have a working SessionsController, however I noticed with the RegisterController that I certainly missed something about the duo Rails-API+Devise. I still get the following error : NameError (undefined local variable or method 'flash' for # <RegistrationsController:0x007ff6022b44b8>)` From a pure API perspective should I

How to rescue ActionDispatch::ParamsParser::ParseError and return custom API error in rails 5?

安稳与你 提交于 2019-12-10 04:04:47
问题 Whenever sending malformed JSON against my API-only Rails 5.x application, I get an exception and Rails is returning the entire stack trace as JSON. Obviously I'd like to respond with a nicely, custom, formatted error. => Booting Puma => Rails 5.0.0.1 application starting in development on http://localhost:3000 => Run `rails server -h` for more startup options Puma starting in single mode... * Version 3.6.0 (ruby 2.3.0-p0), codename: Sleepy Sunday Serenity * Min threads: 5, max threads: 5 *

Using rails_admin with rails_api

泄露秘密 提交于 2019-12-09 09:37:44
问题 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',

Check origin in Rails 5 API

孤者浪人 提交于 2019-12-08 14:09:06
问题 I'm building an app with Rails 5 API. Currently, anyone sending request to my rails server can receive response. I want to process only those requests whose origin is mydomain.com How can I do so? 回答1: I believe you'll want to implement CORS on your API. Simply add rack-cors gem. And add: #config/application.rb config.middleware.insert_before 0, Rack::Cors do allow do origins 'mydomain.com' resource '*', :headers => :any, :methods => [:get, :post, :options] end end Please read carefully its

Why I'm getting undefined method `cookies'?

空扰寡人 提交于 2019-12-08 03:27:51
问题 I'm trying to setup Authlogic with single_access_token for my rails backend app (I'm using rails-api gem). I'm following this example: http://blog.centresource.com/2013/06/04/using-ember-auth-with-rails-3-and-authlogic/ So far, I'm able to create users, but when I try to login it fails on user_session.save : NoMethodError in Api::V1::UserSessionsController#create undefined method `cookies' for #<Api::V1::UserSessionsController:0x00000004528cb8> According to similar questions I've found here,

Using Rails 5 (the API flag) and getting no route error despite route declared

空扰寡人 提交于 2019-12-07 01:15:13
问题 This is my controller: class Api::V1::UsersController < ApplicationController respond_to :json def show respond_with User.find(params[:id]) end end This is my routes.rb require 'api_constraints' Rails.application.routes.draw do devise_for :users # Api definition namespace :api, defaults: { format: :json }, constraints: { subdomain: 'api' }, path: '/' do scope module: :v1, constraints: ApiConstraints.new(version: 1, default: true) do resources :users, :only => [:show] end end end This is my

Force all received requests Content-Type to JSON

北战南征 提交于 2019-12-05 13:40:41
问题 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

CarrierWave full url for default image

╄→гoц情女王★ 提交于 2019-12-05 10:35:37
I am using CarrierWave for image uploads on a rails-api application which in turn is consumed by a backbone.js client app. I notice that when there is no default image available it returns /assets/default.png . It in turn has to be http://dev-server:3000/assets/default.png . Here are my configuration settings: # config/environments/development.rb CarrierWave.configure do |config| config.asset_host = "http://dev-server:3000" end # Image Uploader .... def default_url "/assets/default.png" end Where am I going wrong? [Edit (updated answer)] Updating my answer to setup asset_host in rails config.