[英]My image is uploading, but to nowhere it seems (it's not showing at all) - haven't a clue where I'm going wrong
Currently using PaperClip to upload an image file. 目前使用PaperClip上传图像文件。 In my form I am easily able to update/edit or create a new listing. 在我的表单中,我可以轻松更新/编辑或创建新的列表。 When I upload an image it only shows up as a broken link and when I click 'open image in new tab' I get a message saying: 当我上传图片时,它只会显示为断开的链接,当我点击“在新标签中打开图片”时,我会收到一条消息:
No route matches [GET] "/assets/images/missing.png 没有路线匹配[GET]“/assets/images/missing.png
Am utterly stuck and have no idea whatsoever of what to do next. 我完全陷入困境,不知道接下来该做什么。 Would be truly grateful if someone could please suggest some pointers or tips on what to do? 如果有人能提出一些关于该怎么做的建议或提示,真的很感激吗?
EDIT: 编辑:
This is my controller: 这是我的控制器:
class ListingsController < ApplicationController
before_action :set_listing, only: [:show, :edit, :update, :destroy]
# GET /listings
# GET /listings.json
def index
@listing = Listing.all
end
# GET /listings/1
# GET /listings/1.json
def show
end
# GET /listings/new
def new
@listing = Listing.new
end
# GET /listings/1/edit
def edit
end
# POST /listings
# POST /listings.json
def create
@listing = Listing.create( listing_params )
respond_to do |format|
if @listing.save
format.html { redirect_to @listing, notice: 'Listing was successfully created.' }
format.json { render action: 'show', status: :created, location: @listing }
else
format.html { render action: 'new' }
format.json { render json: @listing.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /listings/1
# PATCH/PUT /listings/1.json
def update
@listing = Listing.find(params[:id]) #find item you want to update
respond_to do |format|
if @listing.update_attributes(listing_params[:id]) # the refer to it and update it
format.html { redirect_to @listing, notice: 'Listing was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @listing.errors, status: :unprocessable_entity }
end
end
end
# DELETE /listings/1
# DELETE /listings/1.json
def destroy
@listing.destroy
respond_to do |format|
format.html { redirect_to listings_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_listing
@listing = Listing.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def listing_params
params.require(:listing).permit(:name, :description, :price, :image)
end
end
Here is my model - I just wanted to upload the file first, irrespective of size. 这是我的模型 - 我只是想首先上传文件,无论大小。
class Listing < ActiveRecord::Base
has_attached_file :image,
:styles => { :medium => "200x200", :thumb => "100x100>" }
end
Here is my development log: 这是我的开发日志:
Started GET "/assets/turbolinks.js?body=1" for 127.0.0.1 at 2014-06-09 00:23:48 +0100
Started GET "/assets/listings.js?body=1" for 127.0.0.1 at 2014-06-09 00:23:48 +0100
Started GET "/assets/pages.js?body=1" for 127.0.0.1 at 2014-06-09 00:23:48 +0100
Started GET "/assets/application.js?body=1" for 127.0.0.1 at 2014-06-09 00:23:48 +0100
Started GET "/images/original/missing.png" for 127.0.0.1 at 2014-06-09 00:23:48 +0100
ActionController::RoutingError (No route matches [GET] "/images/original/missing.png"):
actionpack (4.0.0) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.0.0) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.0.0) lib/rails/rack/logger.rb:21:in `block in call'
activesupport (4.0.0) lib/active_support/tagged_logging.rb:67:in `block in tagged'
activesupport (4.0.0) lib/active_support/tagged_logging.rb:25:in `tagged'
activesupport (4.0.0) lib/active_support/tagged_logging.rb:67:in `tagged'
railties (4.0.0) lib/rails/rack/logger.rb:21:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
rack (1.5.2) lib/rack/runtime.rb:17:in `call'
activesupport (4.0.0) lib/active_support/cache/strategy/local_cache.rb:83:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/static.rb:64:in `call'
railties (4.0.0) lib/rails/engine.rb:511:in `call'
railties (4.0.0) lib/rails/application.rb:97:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
rack (1.5.2) lib/rack/content_length.rb:14:in `call'
rack (1.5.2) lib/rack/handler/webrick.rb:60:in `service'
C:/Ruby200/lib/ruby/2.0.0/webrick/httpserver.rb:138:in `service'
C:/Ruby200/lib/ruby/2.0.0/webrick/httpserver.rb:94:in `run'
C:/Ruby200/lib/ruby/2.0.0/webrick/server.rb:295:in `block in start_thread'
Rendered C:/Ruby200/lib/ruby/gems/2.0.0/gems/actionpack- 4.0.0/lib/action_dispatch/middleware/templates/rescues/_trace.erb (2.0ms)
Rendered C:/Ruby200/lib/ruby/gems/2.0.0/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/routes/_route.html.erb (2.0ms)
Rendered C:/Ruby200/lib/ruby/gems/2.0.0/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/routes/_table.html.erb (1.0ms)
Rendered C:/Ruby200/lib/ruby/gems/2.0.0/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (116.0ms)
HI @USERsomenumbers... HI @USERsomenumbers ...
In the Documetation for Parperclip . 在Parperclip的Documetation中 。 They showed an example of how should look your Model (listing.rb) 他们展示了一个如何看待你的模型的例子(listing.rb)
class Listing < ActiveRecord::Base
has_attached_file :image, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "http://i.kinja-img.com/gawker-media/image/upload/s--iEV2_cUo--/c_fill,fl_progressive,g_north,h_77,q_80,w_137/akjmndys0n7jgpfmf41d.jpg"
end
The :default_url
option stands for a replacement in case that a user didn't upload an image and if you didn't write it in your Model it will have a default url ( '/images/thumb/missing.png'
... or something similar) but you'll have an error cause there is no routes (link) in your app that has that file. :default_url
选项代表替换,以防用户没有上传图像,如果你没有在你的模型中写它,它将有一个默认的网址( '/images/thumb/missing.png'
...或类似的东西)但你会有一个错误,因为你的应用程序中没有路由(链接)有该文件。 So you should always write a default url OR add a Validation (:presence true) to force the user to upload an image 因此,您应始终编写默认URL或添加验证(:presence true)以强制用户上载图像
Also...to verify if your app did save your upload you should log in the console ( rails c -s
) and Listing.last
另外......要验证您的应用是否确实保存了您的上传内容,您应该登录控制台( rails c -s
)和Listing.last
2.1.0 :001 > Listing.last
Listing Load (0.3ms) SELECT "listings".* FROM "listings" ORDER BY "listings"."id" DESC LIMIT 1
=> #<Listing id: 3, name: "34", description: "54", price: #<BigDecimal:93f0120,'0.77E2',9(27)>, created_at: "2014-06-09 06:02:20", updated_at: "2014-06-09 06:02:20", image_file_name: nil, image_content_type: nil, image_file_size: nil, image_updated_at: nil>
As you see for all the symbol that starts with :image
the value is nil
so it means the user didn't upload an image and that rails will look for an image that is set in a :default_url
正如您所看到的所有开头的符号:image
该值为nil
,这意味着用户没有上传图像,并且rails将查找在以下设置中设置的图像:default_url
Also you should have this line on your _form.html.erb 你也应该在_form.html.erb上有这一行
<%= form_for @listing, :url => listings_path, :html => { :multipart => true } do |f| %>
I am not sure if rails saved an image path 我不确定rails是否保存了图像路径
Rails / Paperclip will not save an image path - Paperclip will have a folder / file structure where it will save the files you need (this is defaulted to public/system
) Rails / Paperclip不会保存图像路径 - Paperclip将有一个文件夹/文件结构,它将保存您需要的文件(默认为public/system
)
Paperclip works by taking your file, converting it to an object & saving the following pieces of information to your database: Paperclip通过获取您的文件,将其转换为对象并将以下信息保存到您的数据库来工作:
- attachment_file_name attachment_file_name
- attachment_content_type attachment_content_type
- attachment_file_size attachment_file_size
- attachment_updated_at attachment_updated_at
This means your problem is not likely to be with a lack of image path
from Paperclip. 这意味着您的问题不太可能是Paperclip缺少image path
。 Paperclip is literally just a way to append an image object to your record, allowing you to do something like this: Paperclip实际上只是一种将图像对象附加到记录中的方法,允许您执行以下操作:
@post = Post.find 1
@post.image.url
Validation 验证
I think your problem is with your validation 我认为您的问题与您的验证有关
Paperclip 4 introduced a new type of validation, which checks file spoofing before uploading (basically checks the file type). Paperclip 4引入了一种新类型的验证,它在上传之前检查文件欺骗(基本上检查文件类型)。 The way to fix this is to use the following validation: 解决此问题的方法是使用以下验证:
#app/models/listing.rb
class Listing < ActiveRecord::Base
has_attached_file :image, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"
validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/
end
Your other code looks good - I would suggest the problem is your file is not being validated correctly, leading it to not get saved. 你的其他代码看起来不错 - 我建议问题是你的文件没有被正确验证,导致它没有得到保存。 Either that, or you're calling the file incorrectly 要么是这样,要么是你错误地调用了文件
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.