【问题标题】:Json uploading image to RoR-Server: NoMethodError undefined method `permit'Json 将图像上传到 RoR-Server:NoMethodError 未定义方法 `permit'
【发布时间】:2017-01-18 20:30:02
【问题描述】:

我想将图像从 android 应用程序上传到 Ruby on Rails 服务器,但出现错误,提示 NoMethodError undefined method `permit'。我认为我的参数有问题。 RoR 接收这些参数:

  Parameters: {"document"=>#<ActionDispatch::Http::UploadedFile:0x53c1b10 @tempfile=#<Tempfile:C:/Users/Clemens/AppData/Local/Temp/RackMultipart20160911-9268-qqwltt.jpg>, @original_
filename="IMG_20160911_122254.jpg", @content_type="application/octet-stream", @headers="Content-Disposition: form-data; name=\"document\"; filename=\"IMG_20160911_122254.jpg\"\r\nCo
ntent-Type: application/octet-stream\r\nContent-Transfer-Encoding: binary\r\n">}

这是我的 documents_controller.rb

class DocumentsController < ApplicationController
  before_action :set_document, only: [:show, :edit, :update, :destroy]
 skip_before_filter :verify_authenticity_token

  # GET /documents
  # GET /documents.json
  def index
    @documents = Document.all
  end

  # GET /documents/1
  # GET /documents/1.json
  def show
    send_data(@document.file_contents,
              type: @document.content_type,
              filename: @document.filename)
  end

  # GET /documents/new
  def new
    @document = Document.new
  end

   #POST /documents
  #POST /documents.json
  def create
    @document = Document.new(document_params)

    respond_to do |format|
      if @document.save
        format.html { redirect_to documents_path, notice: 'Document was successfully created.' }
        format.json { render action: 'show', status: :created, location: @document }
      else
        format.html { render action: 'new' }
        format.json { render json: @document.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /documents/1
  # PATCH/PUT /documents/1.json
  def update
    respond_to do |format|
      if @document.update(document_params)
        format.html { redirect_to @document, notice: 'Document was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @document.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /documents/1
  # DELETE /documents/1.json
  def destroy
    @document.destroy
    respond_to do |format|
      format.html { redirect_to documents_url }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_document
      @document = Document.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def document_params
      params.require(:document).permit(:file)
    end

end

application_controller.rb

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
 ############ protect_from_forgery with: :exception
  protect_from_forgery with: :null_session



end

models/document.rb

class Document < ActiveRecord::Base

  validate :file_size_under_one_mb

  def initialize(params = {})
    @file = params.delete(:file)
    super
    if @file
      self.filename = sanitize_filename(@file.original_filename)
      self.content_type = @file.content_type
      self.file_contents = @file.read
    end
  end

  private

    def sanitize_filename(filename)
      # Get only the filename, not the whole path (for IE)
      # Thanks to this article I just found for the tip: http://mattberther.com/2007/10/19/uploading-files-to-a-database-using-rails
      return File.basename(filename)
    end

    NUM_BYTES_IN_MEGABYTE = 1048576
    def file_size_under_one_mb
     if (@file.size.to_f / NUM_BYTES_IN_MEGABYTE) > 1
        errors.add(:file, 'File size cannot be over one megabyte.')
      end
    end

end

当我尝试上传图片时,我收到以下错误消息:

NoMethodError (undefined method `permit' for #<ActionDispatch::Http::UploadedFile:0x59553c0>):

app/controllers/documents_controller.rb:72:in `document_params'
app/controllers/documents_controller.rb:27:in `create'
  Rendering C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout
  Rendering C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_source.html.erb
  Rendered C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_source.html.erb (5.0ms)
  Rendering C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb
  Rendered C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (3.0ms)
  Rendering C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb
  Rendered C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.0ms)
  Rendered C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (840.0
ms)

我想了很久。我怎么解决这个问题?我的建议是,Json 提供一个字符串,但我的 ruby​​ on rails 程序需要一个哈希。我能做什么?

【问题讨论】:

    标签: ruby-on-rails json image-uploading


    【解决方案1】:

    这里的问题是"document" 的值不是哈希值而是ActionDispatch 的实例,这就是您收到错误的原因。

    permit 可以应用于哈希而不是实例。将document_params方法改为

    def document_params 
      params.permit(:document) 
    end 
    

    【讨论】:

    • 我做到了,比你,我可以上传图片文件。但是,当我想从 ruby​​ on rails 下载文件时,我在 DocumentsController#show: :type 选项中得到一个 ArgumentError 。出了点问题,我该如何解决?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多