Rails 回形针不显示图像 [英] Rails paperclip not showing image
问题描述
我是回形针的新手,我想看看它是如何工作的.我生成了一个简单的模型 Monkey 并得到以下内容:
rails g 脚手架猴子描述 age:datetimerails g 回形针猴子 facepic耙数据库:迁移
型号
class Monkey{:中 =>"300x300>", :thumb =>100x100"}
结束
查看新的/编辑
<%= form_for @monkey, :url =>Monkeys_path, :html =>{ :multipart =>真 } 做 |f|%>...<div class="field"><%= f.label :facepic %><br><%= f.file_field :facepic %>
观看节目
<%= image_tag @monkey.facepic.url %>
控制器
@monkey = Monkey.new(monkey_params)
我可以创建新的猴子,但显示视图似乎没有找到上传的文件.我没有错误消息,除了missing.png"的路由错误.上传的图像没有任何痕迹.我正在使用 Rails 4.1.6.我在这里缺少什么?我该如何解决这个问题?安装了 gem 并安装了 imagemagick.
日志是这样写的:
ActionController::RoutingError(没有路由匹配 [GET] "/facepics/original/missing.png"):...在 2014-09-19 14:40:22 +0200 开始为 127.0.0.1 获取/monkies/new"由 MonkiesController#new 处理为 HTML渲染的猴子/_form.html.erb (4.0ms)在布局/应用程序中渲染 Monkis/new.html.erb(5.0 毫秒)在 12 毫秒内完成 500 个内部服务器错误ActionView::Template::Error(没有路由匹配 {:action=>"show", :controller=>"monkies"} 缺少必需的键:[:id]):
但是当我创建一个新的猴子时没有显示错误消息... :'(
猴子模型已创建,但回形针列仍为空.
此错误清楚地表明您的图像没有被保存,因为在您的 has_attached_file
块中未指定路径和 url.应该是这样的:
has_attached_file :facepic,:路径=>":rails_root/public/system/:attachment/:id/:style/:filename",:url =>"/system/:attachment/:id/:style/:filename",:styles =>{:中 =>"300x300>", :thumb =>100x100"},:default_url =>默认图像的路径"
如果没有上传图片,这里 default_url 显示您想要的图片.有关更多详细信息,您可以访问这里 http://rdoc.info/gems/paperclip/4.2.0/Paperclip/ClassMethods%3ahas_attached_file .
对于其他错误,您可以点击此链接 Paperclip::Errors::MissingRequiredValidatorError with Rails 4一个>
I am new to paperclip, and I wanted to see how it would work. I generated a simple model Monkey and got the following:
rails g scaffold monkey description age:datetime
rails g paperclip monkey facepic
rake db:migrate
Model
class Monkey< ActiveRecord::Base
has_attached_file :facepic, :styles => { :medium => "300x300>", :thumb => "100x100>" }
end
View new/edit
<%= form_for @monkey, :url => monkies_path, :html => { :multipart => true } do |f| %>
...
<div class="field">
<%= f.label :facepic %><br>
<%= f.file_field :facepic %>
</div>
View show
<%= image_tag @monkey.facepic.url %>
Controller
@monkey = Monkey.new(monkey_params)
I can create new monkeys, but the show view doesn't seem to find the uploaded file. I have no error messages, except a routing error to 'missing.png'. There is no trace to the uploaded image. I am using Rails 4.1.6. What am I missing here? How do I troubleshoot this thing? The gem is installed and also imagemagick is installed.
This is what the logs say:
ActionController::RoutingError (No route matches [GET] "/facepics/original/missing.png"):
...
Started GET "/monkies/new" for 127.0.0.1 at 2014-09-19 14:40:22 +0200
Processing by MonkiesController#new as HTML
Rendered monkies/_form.html.erb (4.0ms)
Rendered monkies/new.html.erb within layouts/application (5.0ms)
Completed 500 Internal Server Error in 12ms
ActionView::Template::Error (No route matches {:action=>"show", :controller=>"monkies"} missing required keys: [:id]):
There is no error message displayed however when I create a new monkey... :'(
EDIT:
The Monkey model is created, but the paperclip columns remain empty.
This error clearly show that your images are not getting saved because path and url not specified in your has_attached_file
block. It should be like :
has_attached_file :facepic,
:path => ":rails_root/public/system/:attachment/:id/:style/:filename",
:url => "/system/:attachment/:id/:style/:filename",
:styles => { :medium => "300x300>", :thumb => "100x100>" },
:default_url => "path to default image"
here default_url show image that you want if no image uploaded. For more detail you can go here http://rdoc.info/gems/paperclip/4.2.0/Paperclip/ClassMethods%3ahas_attached_file .
And for other error you can follow this link Paperclip::Errors::MissingRequiredValidatorError with Rails 4
这篇关于Rails 回形针不显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!