Tag: rails api

常规Rails应用程序和Rails API之间有什么区别?

在学习Rails的过程中,我读到了如何将它与一些前端MV * JavaScript框架(如Backbone.js,Angular.js或Ember.js)结合起来以改进UX。 这引入了(对我而言)使用Rails作为API而不是Web应用程序的概念。 所以,现在,我很困惑:常规Rails应用程序和Rails API之间有什么区别?

使用active_model_serializers实现API版本控制的正确方法

我知道已经存在一些问题,而且这是一个关于AMS不能有效处理命名空间 (这种版本控制方法使用) 的开放性问题,但我想确保我在当前约束内处于正确的轨道。 现在我正在使用Rails 5和AMS 0.10.1,所以我做了以下事情: # config/initializers/active_model_serializer.rb ActiveModelSerializers.config.serializer_lookup_enabled = false 禁用默认的序列化程序查找(无论如何都不起作用); 和 # app/controllers/application_controller.rb class ApplicationController < ActionController::API def get_serializer(resource, options = {}) unless options[:each_serializer] || options[:serializer] then serializer = (self.class.name.gsub("Controller","").singularize + "Serializer").constantize resource.respond_to?(:to_ary) ? options[:each_serializer] = serializer : options[:serializer] = serializer end super(resource, options) end end 覆盖默认情况下如何找到序列化程序; 我的控制器和序列化器是这样的: # app/controllers/api/v2/api_controller.rb module Api::V2 class […]

在Rails应用程序中构建2个中间件堆栈

我有一个Rails应用程序提供网站和API。 我不希望API的中间件堆栈中出现一些元素,例如: ActionDispatch::Cookies , ActionDispatch::Session::CookieStore或ActionDispatch::Flash 。 网站的中间件堆栈保持正常。 那我该怎么办呢? 谢谢。

ActionDispatch :: Request的未定义方法`flash’

我正在尝试在Rails 4中编写一个Ember应用程序,并决定使用rails-api作为api控制器,同时保持应用程序控制器完好无损的几页不属于单页面应用程序。 换句话说,这是我的控制器: app/controllers/application_controller.rb : class ApplicationController < ActionController::Base protect_from_forgery end app/controllers/sample_controller.rb : class SampleController < ApplicationController # my methods end app/controllers/api/v1/api_controller.rb : class Api::V1::ApiController < ActionController::Api include ActionController::MimeResponds end app/controllers/api/v1/sample_controller.rb : module Api::V1 class SampleController < ApiController respond_to :json # my methods end end 我的application.html.slim包含以下行: == render partial: “flash_msgs” unless flash.blank? 包含哪个会导致以下错误: 未定义的方法’flash’用于# 根据这个线程的讨论,似乎罪魁祸首可能是rails-api […]

设计用户sign_in为CSRF令牌真实性令牌提供身份validation错误

我正在使用带有rails devise (最新版本 – 3.2.0)(最新版本 – 4.0.1) 我正在进行简单的身份validation(没有ajax或api)并且获得CSRF真实性令牌的错误。 检查下面的POST请求 started POST “/users/sign_in” for 127.0.0.1 at 2013-11-08 19:48:49 +0530 Processing by Devise::SessionsController#create as HTML Parameters: {“utf8″=>”✓”, “authenticity_token”=>”SJnGhXXUXjncnPhCdg3muV2GYCA8CX2LVFV78pqddD4=”, “user”=> {“email”=>”a@a.com”, “password”=>”[FILTERED]”, “remember_me”=>”0”}, “commit”=>”Sign in”} Can’t verify CSRF token authenticity User Load (0.4ms) SELECT “users”.* FROM “users” WHERE “users”.”email” = ‘a@a.com’ LIMIT 1 (0.1ms) begin transaction SQL (0.4ms) […]

为什么我得到未定义的方法`cookies’?

我正在尝试使用single_access_token为我的rails后端应用程序设置Authlogic(我正在使用rails-api gem)。 我正在关注这个例子: http : //blog.centresource.com/2013/06/04/using-ember-auth-with-rails-3-and-authlogic/ 到目前为止,我能够创建用户,但是当我尝试登录时,它在user_session.save上失败 : NoMethodError in Api::V1::UserSessionsController#create undefined method `cookies’ for # 根据我在这里找到的类似问题,我必须将Authlogic配置为: acts_as_authentic do |c| c.login_field = :email c.maintain_sessions = false end 在我的用户模型上。 然而,它一直在失败。 我假设当UserSession被保存时,Authlogic尝试更新User上的魔术字段,但我不知道为什么还在尝试使用cookie。

Rspec表示,为什么机架式磁盘不能过滤传入的请求

我希望我的Rails 5 API专用应用程序,现在运行在http://localhost:3000 ,只接受来自我的NodeJS前端应用程序的请求,现在运行在http://localhost:8888 。 所以我像这样配置了/config/initializers/cors.rb : Rails.application.config.middleware.insert_before 0, Rack::Cors do allow do origins “http://localhost:8888” resource “*”, headers: :any, methods: [:get, :post, :put, :patch, :delete, :options, :head] end end 我写了这个测试: #/spec/request/cors_request_spec.rb RSpec.feature “CORS protection”, type: :request do it “should accept a request from a whitelisted domain” do get “/api/v1/bodies.json”, nil, “HTTP_ORIGIN”: “http://localhost:8888” expect(response.status).to eql(200) end […]

过滤链停止为:validate_sign_up_params呈现或重定向 – devise_token_auth

我正在使用devise_token_auth作为身份validation来处理 rails API上的ruby,每次我从postman google chrom扩展程序进行API调用时我都会收到此错误。下面是我的设置 的Gemfile gem ‘devise’ gem ‘omniauth’ gem ‘devise_token_auth’ gem ‘byebug’ gem ‘rack-cors’, :require => ‘rack/cors’ 的routes.rb mount_devise_token_auth_for ‘User’, at: ‘auth’ application_controller.rb include DeviseTokenAuth::Concerns::SetUserByToken before_action :authenticate_user! , :except=>[:new, :create] 用户模型: devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable, :confirmable, :omniauthable include DeviseTokenAuth::Concerns::User after_initialize :set_provider #, :set_uid def set_provider byebug self[:provider] = “email” if […]

Ruby API – 接受参数并执行脚本

我创建了一个rails项目,其中包含一些我想作为API执行的代码。 我正在使用rails-api gem。 该文件位于app / controllers / api / stats.rb中。 我希望能够执行该脚本并通过访问此类链接返回json输出 – http://sampleapi.com/stats/?location=USA?state=Florida 。 我应该如何配置我的项目,以便当我访问该链接时它运行我的代码?

rails 5 API低级缓存

我对Rails API缓存有点困惑。 我正在使用JSONAPI规范和fast_jsonapi gem并尝试在show动作上缓存车辆本身,如果有像para include=service_notes,service_alerts这样的params include=service_notes,service_alerts那么我也想缓存它们。 这是我最初的方法,但不确定它是否正确。 我有两个主要问题: 对于车辆缓存本身有一个比我的车辆更好的方法= Vehicle.find_cached(params [:id])。 如果车辆已更新,则不使用updated_at,而是使用after save回调来更新缓存。 我只是不知道我是否能以某种方式使用像Rails.cache.fetch([“vehicles”, vehicle], version: vehicle.updated_at)因为它在这里建议: https : //github.com/rails/rails / pull / 29092因为这需要车辆实例。 如您所见, set_vehicle控制器方法非常尴尬。 这个Rails.cache.fetch([‘vehicles’, vehicle, include_params], version: vehicle.updated_at)是否有意义? 我试图根据不同的包括参数缓存查询。 也许它有点矫枉过正,我可以只包括所有内容并将其缓存为: Rails.cache.fetch([‘vehicles’,vehicle,’with_includes’],version:vehicle.updated_at)执行Vehicle.includes(:vehicle_alerts,:service_notes,:service_intervals).find(params [:id])end 在这里处理缓存的正确方法是什么? service_note.rb为service_interval.rb和vehicle_alert.rb设置相同 class ServiceNote < ApplicationRecord belongs_to :vehicle, touch: true end vehicle.rb class Vehicle < ApplicationRecord after_save :update_cache has_many :vehicle_alerts, […]