我正在开发一个用户可以从硬盘上传图片的应用程序。我使用回形针将这些图片保存到Amazon S3。我还保存了用户上传图片时每张图片的经度和纬度。我是用地理编码器做的。一切都运作良好,自从我上次尝试上传新图片以来,它给我带来了以下错误:
NoMethodError(未定义的方法address=' for #<Upload:0x007f8b13194b50>):
app/controllers/uploads_controller.rb:16:in
创建&#39;
我无法找到问题,我正在使用我正在使用的控制器和型号。
控制器
class UploadsController < ApplicationController
def index
@uploads = Upload.near([request.location.latitude, request.location.longitude], 1)
# render json: Upload.all.to_json(:methods => [:image_url])
render json: @uploads.to_json(:methods => [:image_url])
end
def new
@upload = Upload.new
end
def create
@upload = Upload.create(upload_params)
if @upload.save
render json: { message: "success" }, :status => 200
else
#need to send an error header, otherwise Dropzone
# will not interpret the response as an error:
render json: { error: @upload.errors.full_messages.join(',')}, :status => 400
end
end
private
def upload_params
params.require(:upload).permit(:image, :latitude, :longitude)
end
end
模型
class Upload < ActiveRecord::Base
# geocoded_by :ip_address,
# :latitude => :lat, :longitude => :lon
# after_validation :geocode
# geocoded_by :address
# after_validation :geocode
reverse_geocoded_by :latitude, :longitude
after_validation :reverse_geocode # auto-fetch address
has_attached_file :image, :styles => { :medium => "300x300>",:thumb => "100x100>" }
validates_attachment :image,
:presence => true,
:content_type => { :content_type => /\Aimage\/.*\Z/ },
:size => { :less_than => 2.megabyte }
def image_url
image.url
end
end
答案 0 :(得分:0)
也许你需要重新启动你的Rails服务器,或者你可能没有真正粘贴你正在运行的代码。
您的控制器的第16行是Upload.create(upload_params)
行,根据您在模型中注释的geocoded_by :address
代码,我猜测您有:address
。 }作为您允许的上传参与号之一,如果仍然在upload_params
和您的表单中,那么这将解释为什么在address=
内调用Upload.create