我想将一个图像从Android应用程序上传到Ruby on Rails服务器,但是我收到一个错误,说明NoMethodError未定义的方法`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
模型/ 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程序需要一个哈希。我该怎么办?
答案 0 :(得分:1)
此处的问题是"document"
的值不是哈希值,而是ActionDispatch
的实例,这就是您收到错误的原因。
permit
可以应用于哈希非实例。将document_params
方法更改为
def document_params
params.permit(:document)
end