2013-10-29 63 views
1

我安裝了Paperclip gem,用它在我的模型中生成:image列。然後編輯觀點讓我上傳的照片:Rails - 回形針不上傳照片?顯示missing.png

<%= form_for @book = Book.new, :html => { :multipart => true } do |f| %> 
... 
... 
... 

<%= f.file_field :image %> 

在我的模型我進入這一行:

has_attached_file :image, :styles => { :smal => "200x200>"} 
        :url => "/assets/books/:id/:style/:basename.:extension", 
        :path => ":rails_root/public/assets/books/:id/:style/:basename.:extension" 

我可以選擇上傳照片,當我上傳它,它給了錯誤的missing.png ,即使圖像名稱不是這樣,但似乎照片根本沒有上傳。什麼可能是錯的? 編輯:完整的錯誤:

No route matches [GET] "/assets/books/17/small/thunderbird-logo-200x200.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' 
    /usr/local/lib/ruby/2.0.0/webrick/httpserver.rb:138:in `service' 
    /usr/local/lib/ruby/2.0.0/webrick/httpserver.rb:94:in `run' 
    /usr/local/lib/ruby/2.0.0/webrick/server.rb:295:in `block in start_thread' 


    Rendered /usr/local/lib/ruby/gems/2.0.0/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/_trace.erb (5.9ms) 
    Rendered /usr/local/lib/ruby/gems/2.0.0/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/routes/_route.html.erb (2.5ms) 
    Rendered /usr/local/lib/ruby/gems/2.0.0/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/routes/_table.html.erb (1.3ms) 
    Rendered /usr/local/lib/ruby/gems/2.0.0/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (27.2ms) 
+0

你可以粘貼實際的圖像。上傳不會給你這個消息。 Missing.png是當圖像不存在於使用回形針的模型時默認使用的圖像名稱。聽起來像你有其他驗證失敗或您的輸出代碼不正確 – cpjolicoeur

+0

它似乎是上傳圖像,但是當它需要顯示它時,它無法找到它上傳的位置。它上傳到/ public/system/books/images/000/000/013 /小。 – Andrius

+0

它給出了這個錯誤: '沒有路線匹配[GET]「/ system/books/images/000/000/013/small/thunderbird-logo-200x200 .png「' – Andrius

回答

0

嘗試明確設置

has_attached_file :image, 
     :path => ":rails_root/public/system/:attachment/:id/:style/:filename", 
     :url => "/system/:attachment/:id/:style/:filename", 
     :styles => { :smal => "200x200>" } 
+0

試過了。沒有工作 – Andrius

+0

我認爲它與路由有關。即使我設置了整個路徑,它仍然會引發路徑錯誤。 – Andrius

+0

您可以使用您設置的路徑和網址更新您的問題嗎? – trh

2

您可以在您添加修復這個錯誤的路徑和URL控制器私人service_params

位指示:

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

# Never trust parameters from the scary internet, only allow the white list through. 
def service_params 
    params.require(:service).permit(:title, :about, :price, :photo) 
end 

您必須添加:照片到:標題,:約,:價格(或什麼李什麼你有)

+0

添加新字段以將params添加到控制器中後,非常常見的錯誤。謝謝! – avdyushin

相關問題