我是Postman的新手。我有一个在后台运行的Rails服务器。我正在尝试模拟POST请求,但它没有被接受。
假设模型名为manufacturer_organization.rb
。在内部,它需要 3个参数:organization_id (uuid data type), manufacturer_id (integer data type), and account_number (string data type).
manufacturer_organization
belongs_to组织,它也属于:制造商(反之亦然;制造商和组织has_many manufacturer_organization)
在manufacturer_organizations_controller.rb
内,我有一个创建方法:
def create
@manufacturer_organization = ManufacturerOrganization.new(manufacturer_organization_params)
if @manufacturer_organization.save
puts "success!"
render json: @manufacturer_organization
else
puts "Sorry, something went wrong"
end
end
我可以确认我有足够的授权;当我执行GET请求时,我得到了正确的JSON响应。我正在使用rails serializer,我也为这个模型设置了序列化器。路线也使用resources :manufacturer_organizations
设置。我的直觉是说我使用邮递员的方式是错误的。
这是Postman应用程序的屏幕截图。我在地址栏上有正确的地址,我正在执行POST请求。我在key-value
下面有三个参数。
在Send
之后,在我的Rails服务器日志下,我看到:
Started POST "/manufacturer_organizations" for 127.0.0.1 at 2017-04-13 16:56:44 -0700
Processing by ManufacturerOrganizationsController#create as */*
Parameters: {"organization_id"=>"fb20ddc9-a3ee-47c3-bdd2-f710541-ff89c", "manufacturer_id"=>"1", "account_number"=>"A rand
om account number test"}
...
(0.4ms) BEGIN
(0.3ms) ROLLBACK
Sorry, something went wrong
我可以在ManufacturerOrganization.new(organization_id: Organization.last.id, manufacturer_id: Manufacturer.last.id, and account_number: "random test account number")
内完成rails console
。
如何从邮递员提交POST请求以添加新的manufacturer_organization?
修改
def manufacturer_organization_params
api_params.permit(:organization_id, :manufacturer_id, :account_number)
end
而在application_controller.rb
def api_params
@api_params ||= ActionController::Parameters.new(ActiveModelSerializers::Deserialization.jsonapi_parse(params))
end
EDIT2:
我添加了error.full_messages
,这就是我得到的:
Manufacturer can't be blank
Organization can't be blank
Account number can't be blank
他们为什么空白?
答案 0 :(得分:0)
您可以使用参数或在正文请求中传递数据。
执行此操作的最佳方法是使用正文,因为您可以发送文件,并且请求变得更干净而没有参数。
要在正文中发送数据,必须在“键”字段中传递模型名称和属性,并在“值”字段中传递值,如下所示:
答案 1 :(得分:0)
我不明白您对 params 所做的工作。在
如果参数以正确的格式 ActionController :: Base 到达,则 AplicationController 从中继承,因此 ManufacturerOrganizationsController 将从中为您反序列化。 Rails查询参数格式如下:
name=something #=> params[:name] = 'something'
names[]=something1&names[]=something2 #=> params[:names] = ['something1', 'something2']
instance[id]=1&instance[name]=foo #=> params[:instance] = {id: '1', name: 'foo'}
这也可以堆叠,并且由Rails用于嵌套资源。示例:
instance[title]=some&instance[nested][name]=thing&instance[nested][ids][]=1&instance[nested][ids][]=2
#=> params[:instance] = {title: 'some', nested: {name: 'thing', ids: ['1', '2']}}
已经说过了,让我们来看一下您的例子。首先,让我们丢弃那些手动构造的参数并遵守约定:
class ManufacturerOrganizationsController
# ...
private
def manufacturer_organization_params
# arriving params should look like this:
#
#=> params = {
# manufacturer_organization: {
# organization_id: 'fb20ddc9-a3ee-47c3-bdd2-f710541-ff89c',
# organization_id: '1',
# account_number: 'A random account number test'
# }
# }
#
# The method #require raises an exception if the provided key
# is not present or has a blank value (with exception of false).
# If the key is found and has a value present than that value is
# returned.
#
params.require(:manufacturer_organization)
.permit(:organization_id, :manufacturer_id, :account_number)
end
end
通过这种方式,让我们发送正确格式的参数:
+--------------------------------------------+---------------------------------------+
| Key | Value |
|--------------------------------------------|---------------------------------------|
| manufacturer_organization[organization_id] | fb20ddc9-a3ee-47c3-bdd2-f710541-ff89c |
| manufacturer_organization[manufacturer_id] | 1 |
| manufacturer_organization[account_number] | A random account number test |
+--------------------------------------------+---------------------------------------+
将这两件事结合起来可以使您成功创建资源。
您应该从中获取的关键信息是,params不是包含应反序列化的所有params的字符串。如果不是这样,则应该已经反序列化了,否则可能会将参数发送错误。