ruby-on-rails - PATCH 获取 json 主体到 Rails
问题描述
我正在通过 将表单 PATCHing 到 Rails fetch
,但在服务器端未正确处理某些属性。
我
5.2.2
在 Ruby上运行 Railsruby 2.5.1p57
当我将数据发布到服务器时,我console.log
在浏览器中得到以下输出:
{id: 10172, weekday: 1, is_only_private: false, is_allow_forced: false, from_time: 08:00, to_time: 09:00, act_ids: [10001, 10002], customer_id: 10000, consultation_id: 10000}
但在服务器端,我可以在控制台上看到这个日志:
Parameters: {"id"=>"10172", "weekday"=>1, "is_only_private"=>false, "is_allow_forced"=>false, "from_time"=>"08:00", "to_time"=>"09:00", "act_ids"=>[10001, 10002], "customer_id"=>"10000", "consultation_id"=>"10000", "timetable"=>{"id"=>"10172", "weekday"=>1, "is_only_private"=>false, "is_allow_forced"=>false, "from_time"=>"08:00", "to_time"=>"09:00"}}
act_ids
timetable
在属性内消失
我的应用程序是混合的,它在相同的路由中使用 HTML 和 JSON(甚至是 XML)响应。
问题:
这个版本的 Rails 还没有解决吗?
解决方法
def timetable_params
my_params = params.require(:timetable).permit :weekday,
:is_only_private,
:is_allow_forced,
:from_time,
:to_time,
act_ids: []
my_params[:act_ids] ||= params[:act_ids]
my_params
end
解决方案
可能您允许它 as permit(..., :to_time, :act_ids)
,但应该允许通过您的代码act_ids: []
中已有的数组timetable_params
在干净的应用程序上,这可以正常工作:
#!/usr/bin/env ruby
# frozen_string_literal: true
require "bundler/inline"
gemfile(!!ENV['INSTALL']) do
source "https://rubygems.org"
gem 'rails', '5.2.2'
end
require "action_controller/railtie"
class TestApp < Rails::Application
config.root = __dir__
config.eager_load = false
config.session_store :cookie_store, key: "cookie_store_key"
secrets.secret_key_base = "secret_key_base"
config.logger = Logger.new($stdout)
Rails.logger = config.logger
end
TestApp.initialize!
TestApp.routes.draw{ resources :timetables, only: :update }
class TimetablesController < ActionController::Base
include Rails.application.routes.url_helpers
wrap_parameters format: [:json, :xml]
def update
render json: timetable_params
end
def timetable_params
params.require(:timetable).permit(:weekday,
:is_only_private,
:is_allow_forced,
:from_time,
:to_time,
act_ids: [])
end
end
require "minitest/autorun"
require "rack/test"
class BugTest < Minitest::Test
include Rack::Test::Methods
def test_returns_success
payload = {
"id"=>"10172", "weekday"=>1, "is_only_private"=>false, "is_allow_forced"=>false,
"from_time"=>"08:00", "to_time"=>"09:00",
"act_ids"=>[10001, 10002],
"customer_id"=>"10000", "consultation_id"=>"10000"
}
patch "/timetables/10172.json", payload.to_json, { 'CONTENT_TYPE' => 'application/json' }
assert last_response.ok?
puts "resp body: #{last_response.body}"
resp = JSON.parse(last_response.body)
assert_includes(resp.keys, "act_ids")
end
private def app
Rails.application
end
end
生产
Run options: --seed 62182
# Running:
I, [2019-06-14T16:04:02.967561 #44749] INFO -- : Started PATCH "/timetables/10172.json" for 127.0.0.1 at 2019-06-14 16:04:02 +0300
I, [2019-06-14T16:04:02.971039 #44749] INFO -- : Processing by TimetablesController#update as JSON
I, [2019-06-14T16:04:02.971161 #44749] INFO -- : Parameters: {"id"=>"10172", "weekday"=>1, "is_only_private"=>false, "is_allow_forced"=>false, "from_time"=>"08:00", "to_time"=>"09:00", "act_ids"=>[10001, 10002], "customer_id"=>"10000", "consultation_id"=>"10000", "timetable"=>{"id"=>"10172", "weekday"=>1, "is_only_private"=>false, "is_allow_forced"=>false, "from_time"=>"08:00", "to_time"=>"09:00", "act_ids"=>[10001, 10002], "customer_id"=>"10000", "consultation_id"=>"10000"}}
D, [2019-06-14T16:04:02.971850 #44749] DEBUG -- : Unpermitted parameters: :id, :customer_id, :consultation_id
I, [2019-06-14T16:04:02.972484 #44749] INFO -- : Completed 200 OK in 1ms (Views: 0.4ms)
resp body: {"weekday":1,"is_only_private":false,"is_allow_forced":false,"from_time":"08:00","to_time":"09:00","act_ids":[10001,10002]}
.
Finished in 0.020124s, 49.6919 runs/s, 149.0757 assertions/s.
1 runs, 3 assertions, 0 failures, 0 errors, 0 skips
推荐阅读
- c# - C# Winform 使用 Interop 与 microsoft Outlook 365 应用程序客户端
- python - 如何在 Dash 应用程序的新行上打印
- autodesk-forge - 如何在伪造查看器中创建虚线标记?
- reactjs - React GitHub 拉取请求面临的问题
- javascript - React Native 如何让用户在关闭应用程序时保持登录状态
- indexing - 赛普拉斯获取包含我具有给定数据属性的元素的索引行
- mysql - MySQL极其简单的日期查询很慢
- ruby - Chef ruby 代码 - .rb 食谱 - 如果检查配置 ['variable'] 的语句存在
- javascript - 在ajax响应后使javascript工作
- javascript - 在Javascript中将日期时间数组转换为x-ticks的有效方法?