【发布时间】:2017-09-10 05:39:53
【问题描述】:
我是 Postman 的新手。我有一个在后台运行的 Rails 服务器。我正在尝试模拟一个 POST 请求,但它没有被接受。
假设模型名为manufacturer_organization.rb。在内部,它需要 3 个参数:organization_id (uuid data type), manufacturer_id (integer data type), and account_number (string data type).manufacturer_organizationbelongs_to 组织和它还属于_to :manufacturer(反之亦然;制造商和组织有_many 制造商_组织)
在manufacturer_organizations_controller.rb里面,我有一个create方法:
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 序列化程序,并且我也为此模型设置了序列化程序。路线也使用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
我可以在rails console 内做ManufacturerOrganization.new(organization_id: Organization.last.id, manufacturer_id: Manufacturer.last.id, and account_number: "random test account number")。
如何提交邮递员的 POST 请求以添加新的制造商组织?
编辑:
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
编辑2:
我添加了error.full_messages,这就是我得到的:
Manufacturer can't be blank
Organization can't be blank
Account number can't be blank
为什么它们是空白的?
【问题讨论】:
-
请显示您的
manufacturer_organization_params -
嗯,看起来不错。并且 params 从邮递员那里顺利通过 - 我认为邮递员没有问题
-
在 else 中打印实际错误而不是“抱歉”消息,看看问题出在哪里。
-
把@manufacturer_organization_params 看一下ManufacturerOrganization 的初始化方法
-
抱歉,您介意详细说明一下吗?我在控制器上添加了
puts manufacturer_organization_params,它打印出<ActionController::Parameters:0x007fc1ce415b80>,而当我提交POST时添加puts @manufacturer_organization_params什么也不打印。
标签: ruby-on-rails api postman