`.允许! ` 未在 Rails 测试环境中定义

问题描述 投票:0回答:1
(byebug) params.require(:user_documents).permit!
#<ActionController::Parameters {"0"=>#<ActionController::Parameters {"document_type_id"=>"12", "file"=>#<ActionDispatch::Http::UploadedFile:0x00007f2c532602b0 @tempfile=#<Tempfile:/tmp/RackMultipart20240917-9912-oqmfr8.pdf>, @original_filename="Tate sir.pdf", @content_type="application/pdf", @headers="Content-Disposition: form-data; name=\"user_documents[0][file]\"; filename=\"Tate sir.pdf\"\r\nContent-Type: application/pdf\r\n">} permitted: true>, "1"=>#<ActionController::Parameters {"document_type_id"=>"13", "file"=>#<ActionDispatch::Http::UploadedFile:0x00007f2c532600f8 @tempfile=#<Tempfile:/tmp/RackMultipart20240917-9912-hnt968.pdf>, @original_filename="Tate sir.pdf", @content_type="application/pdf", @headers="Content-Disposition: form-data; name=\"user_documents[1][file]\"; filename=\"Tate sir.pdf\"\r\nContent-Type: application/pdf\r\n">} permitted: true>} permitted: true>

上面在 Rails 开发服务器中,但相同的代码在测试中出现错误,如下所示

params.require(:user_documents).permit!
*** NoMethodError Exception: undefined method `permit!' for [#<ActionController::Parameters {"file"=>#<ActionDispatch::Http::UploadedFile:0x00007fe16dd2f630 @tempfile=#<Tempfile:/tmp/RackMultipart20240917-11539-91yhjb.pdf>, @original_filename="sample.pdf", @content_type="application/pdf", @headers="content-disposition: form-data; name=\"user_documents[][file]\"; filename=\"sample.pdf\"\r\ncontent-type: application/pdf\r\ncontent-length: 3006720\r\n">, "document_type_id"=>"83"} permitted: false>, #<ActionController::Parameters {"file"=>#<ActionDispatch::Http::UploadedFile:0x00007fe16dd38b68 @tempfile=#<Tempfile:/tmp/RackMultipart20240917-11539-pv7tqg.pdf>, @original_filename="sample1.pdf", @content_type="application/pdf", @headers="content-disposition: form-data; name=\"user_documents[][file]\"; filename=\"sample1.pdf\"\r\ncontent-type: application/pdf\r\ncontent-length: 62102\r\n">, "document_type_id"=>"84"} permitted: false>]:Array
Did you mean?  permutation

nil

下面是控制器方法代码

 def create
        @user_documents = []
        errors = []

        # Ensure that user_documents_params returns a hash of document entries
        byebug
        user_documents_params = params.require(:user_documents).permit!

        ActiveRecord::Base.transaction do
          user_documents_params.each do |_index, doc_params|
            existing_document = @current_user.user_documents.find_by(document_type_id: doc_params[:document_type_id])

            if existing_document
              if existing_document.update(doc_params)
                @user_documents << existing_document
              else
                errors.concat(existing_document.errors.full_messages)
              end
            else
              new_user = @current_user.user_documents.new(doc_params)
              if new_user.save
                @user_documents << new_user
              else
                errors.concat(new_user.errors.full_messages)
              end
            end
          end

          if errors.empty?
            render json: { success: true, message: "Files are uploaded successfully" }
          else
            render json: { errors: errors }, status: :unprocessable_entity
          end
        end
      end

      private

      def user_document_params
        params.require(:user_documents)
      end
      

有谁知道为什么代码在不同环境中表现不同

使用 byebug 检查问题是否仍然存在

ruby-on-rails rspec-rails
1个回答
0
投票

:require
将返回参数value。在这种情况下它将是一个普通数组。 这是 ActionController 方法本身的链接

考虑到

:require
:permit!
都是白名单参数(不同之处在于
:require
也需要它们),我建议您在您的情况下只使用其中一个。

© www.soinside.com 2019 - 2024. All rights reserved.