如何在使用Grape gem时自定义状态代码响应?例如
post do
status = :accepted
@order = Order.find(params[:id])
end
Run Code Online (Sandbox Code Playgroud)
这可以通过错误来实现!({error:'Order not found'},404)但是如何为非错误做到这一点?
我想打开我的Rails 2.3应用程序(在Heroku上托管)给开发人员.我想到了两种方法:
我知道这是一个模糊的问题.你有什么好的文章或建筑模式可以帮助我吗?
谢谢,
凯文
我正在重写(是的,我知道!)一个主要由API驱动的Rails应用程序,使用Grape by Intridea和葡萄招摇宝石来启用Swagger UI进行文档编制.
所以我有一个简单的hello world app运行.似乎可以从swagger测试中发现,虽然它现在正在给出JSON解析错误(我将在接下来看看).我想将swagger UI放入Rails应用程序,指向/swagger_doc.json,以便我构建时可以确保文档正确地构建在一起.
我在哪里将Swagger UI放在rails应用程序中?公共目录?
我正在Grape中编写一个API,但它是独立的,没有Rails或Sinatra或任何东西.我想将app.rb文件拆分为单独的文件.我看过如何在葡萄api app中拆分东西?,但这与Rails有关.
我不知道如何使用模块或类来完成这项工作 - 我确实尝试将不同的文件子类化为我的大文件GrapeApp,但这很难看,我甚至不确定它是否正常工作.最好的方法是什么?
我现在有通过文件夹(拆分版本v1,v2等),但仅此而已.
如您所知,您可以指定路由中需要参数,如下所示:
requires :province, :type => String
但是,我希望能够更改引发的错误,并在未给出参数时提供我自己的错误JSON.
我怎么能这么容易做到?猴子补丁我很好.
编辑:我在第191行看到rescue_from,看起来它可能会有所帮助,但我不知道如何使用它.
https://codeclimate.com/github/intridea/grape/Grape::API
我在葡萄api应用程序中使用active-record 4.0,但由于强参数仅适用于rails控制器,我如何在葡萄api类中允许params
如何使用葡萄实体将参数传递给模型方法?
我想在显示项目时检查current_user是否喜欢某个项目,所以我构建了一个模型user_likes?方法:
class Item
include Mongoid::Document
#some attributes here...
has_and_belongs_to_many :likers
def user_likes?(user)
likers.include?(user)
end
end
Run Code Online (Sandbox Code Playgroud)
但我不知道如何将current_user发送到葡萄实体模型:
module FancyApp
module Entities
class Item < Grape::Entity
expose :name #easy
expose :user_likes # <= How can I send an argument to this guy ?
end
end
end
Run Code Online (Sandbox Code Playgroud)
在葡萄api:
get :id do
item = Item.find(.....)
present item, with: FancyApp::Entities::Item # I should probably send current_user here, but how ?
end
Run Code Online (Sandbox Code Playgroud)
我觉得current_user可能应该从最后一段代码发送,但我无法想象如何做到:(
有什么想法吗 ?谢谢 !
我想批量更新实体的属性.
我怎样才能正确消毒来自葡萄的虫子呢?
这是关于参数的控制台日志:
params.except(:route_info, :token, :id)
=> {"display_number"=>"7"}
[18] pry(#<Grape::Endpoint>)> params.permit(:display_number)
ArgumentError: wrong number of arguments (2 for 0..1)
from /Users/boti/.rvm/gems/ruby-2.0.0-p353@thelocker/gems/hashie-2.0.5/lib/hashie/mash.rb:207:in `default'
[19] pry(#<Grape::Endpoint>)> params.sanitize
=> nil
Run Code Online (Sandbox Code Playgroud) 这是我的API的样子:
module ServiceRequests
class API < Grape::API
version 'v1', using: :path
format :json
namespace :companies do
params do
requires :company_id, type: Integer, desc: "A Company Id"
end
resource :service_requests do
get :all do
Company.find(params[:company_id]).service_requests
end
end
end
end
end
Run Code Online (Sandbox Code Playgroud)
我试图像这样打电话:http://localhost/v1/companies/:company_id/service_requests/all但是当我打这个电话时,我收到了错误No route matches [GET] "/v1/companies/1/service_requests/all"
工作守则:
module ServiceRequests
class API < Grape::API
version 'v1', using: :path
format :json
resource :companies do
params do
requires :company_id, type: Integer, desc: "A Company Id"
end
route_param :company_id do
resource :service_requests do …Run Code Online (Sandbox Code Playgroud) 我一直试图让一个简单的Ember.js应用程序现在发布到Grape API后端几个小时,但我似乎无法让它工作.我知道API有效,因为我可以通过Swagger文档向它发布新记录,并且它们是持久的.我知道API和Ember说得很好,因为我可以从服务器获取所有记录并在页面上与它们进行交互,我知道Ember在真空中工作正常,因为我的记录持久存储到本地存储.
但是,我似乎无法获得POST请求.它总是回来400.我已经正确配置了Rack-Cors,我在前端设置了ActiveModelAdapter,后端设置了ActiveModelSerializer.
这是模型
Contact = DS.Model.extend {
firstName: DS.attr('string'),
lastName: DS.attr('string'),
email: DS.attr('string'),
title: DS.attr('string'),
createdAt: DS.attr('date'),
updatedAt: DS.attr('date')
}
Run Code Online (Sandbox Code Playgroud)
和控制器
ContactsNewController = Ember.ObjectController.extend(
actions:
save: ->
@get('model').save()
cancel: ->
true
)
Run Code Online (Sandbox Code Playgroud)
API的相关部分如下所示
desc 'Post a contact'
params do
requires :first_name, type: String, desc: 'First name of contact'
requires :last_name , type: String, desc: 'Last name of the contact'
requires :email , type: String, desc: 'Email of the contact'
requires :title , type: String, desc: 'Title of the contact' …Run Code Online (Sandbox Code Playgroud)