PATCH将json主体获取到Rails

时间:2019-06-13 16:01:56

标签: ruby-on-rails json ruby strong-parameters

我正在通过fetch将表单修补到Rails,但是某些属性在服务器端无法正确处理。

  

我正在通过Ruby 5.2.2运行Rails ruby 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_idstimetable属性内消失

我的应用程序是混合的,它以相同的路由响应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

1 个答案:

答案 0 :(得分:0)

可能您以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