![](/img/trans.png)
[英]I can't generate migrations - “illegal route the controller must be specified” - where am I going wrong?
[英]My image is uploading, but to nowhere it seems (it's not showing at all) - haven't a clue where I'm going wrong
目前使用PaperClip上傳圖像文件。 在我的表單中,我可以輕松更新/編輯或創建新的列表。 當我上傳圖片時,它只會顯示為斷開的鏈接,當我點擊“在新標簽中打開圖片”時,我會收到一條消息:
沒有路線匹配[GET]“/assets/images/missing.png
我完全陷入困境,不知道接下來該做什么。 如果有人能提出一些關於該怎么做的建議或提示,真的很感激嗎?
編輯:
這是我的控制器:
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
這是我的模型 - 我只是想首先上傳文件,無論大小。
class Listing < ActiveRecord::Base
has_attached_file :image,
:styles => { :medium => "200x200", :thumb => "100x100>" }
end
這是我的開發日志:
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 ...
在Parperclip的Documetation中 。 他們展示了一個如何看待你的模型的例子(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
:default_url
選項代表替換,以防用戶沒有上傳圖像,如果你沒有在你的模型中寫它,它將有一個默認的網址( '/images/thumb/missing.png'
...或類似的東西)但你會有一個錯誤,因為你的應用程序中沒有路由(鏈接)有該文件。 因此,您應始終編寫默認URL或添加驗證(:presence true)以強制用戶上載圖像
另外......要驗證您的應用是否確實保存了您的上傳內容,您應該登錄控制台( 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>
正如您所看到的所有開頭的符號:image
該值為nil
,這意味着用戶沒有上傳圖像,並且rails將查找在以下設置中設置的圖像:default_url
你也應該在_form.html.erb上有這一行
<%= form_for @listing, :url => listings_path, :html => { :multipart => true } do |f| %>
我不確定rails是否保存了圖像路徑
Rails / Paperclip不會保存圖像路徑 - Paperclip將有一個文件夾/文件結構,它將保存您需要的文件(默認為public/system
)
Paperclip通過獲取您的文件,將其轉換為對象並將以下信息保存到您的數據庫來工作:
- attachment_file_name
- attachment_content_type
- attachment_file_size
- attachment_updated_at
這意味着您的問題不太可能是Paperclip缺少image path
。 Paperclip實際上只是一種將圖像對象附加到記錄中的方法,允許您執行以下操作:
@post = Post.find 1
@post.image.url
驗證
我認為您的問題與您的驗證有關
Paperclip 4引入了一種新類型的驗證,它在上傳之前檢查文件欺騙(基本上檢查文件類型)。 解決此問題的方法是使用以下驗證:
#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
你的其他代碼看起來不錯 - 我建議問題是你的文件沒有被正確驗證,導致它沒有得到保存。 要么是這樣,要么是你錯誤地調用了文件
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.