我正在尝试使用rails应用程序处理回形针,到目前为止还没有成功。请帮我这样做;我已将我认为的相关信息纳入下面,并将回答问题。
它将上传的照片的名称保存到数据库中,并且似乎有一条路径:
[13] pry(main)> Order.last
Order Load (0.3ms) SELECT "orders".* FROM "orders" ORDER BY "orders"."id" DESC LIMIT 1
=> #<Order id: 22, quantity: 13, created_at: "2013-02-03 21:47:46", updated_at: "2013-02-03 21:47:46", user_id: 8, drawing_file_name: "friends.jpg", drawing_content_type: nil, drawing_file_size: nil, drawing_updated_at: nil>
[14] pry(main)> Order.last.drawing
Order Load (0.2ms) SELECT "orders".* FROM "orders" ORDER BY "orders"."id" DESC LIMIT 1
=> /system/orders/drawings/000/000/022/original/friends.jpg
然而:
从其他答案如these,似乎我从表格传递的params哈希应该有更多的内容;现在,它只是一个字符串名称,似乎太短了
[2] pry(#<OrdersController>)> params
=> {"utf8"=>"✓", "authenticity_token"=>"eTInEukPQ84uIw3TPIdI+u0zo49dQlY68gxrodIoPdA=", "quantity_field"=>"34", "drawing_field"=>["friends.jpg"], "supplier_list"=>["1", "2"], "commit"=>"Create order", "multipart"=>"true", "action"=>"create", "controller"=>"orders"}
order.rb
class Order < ActiveRecord::Base
attr_accessible :quantity, :drawing
has_attached_file :drawing,
:url => "/:attachment/:id/:style/:basename.:extension",
:path => ":rails_root/public/:attachment/:id/:style/:basename.:extension"
_form.html.erb
<%= form_tag :action => 'create', :controller => 'orders', :multipart => true do %>
....
<div>
<%= file_field :order, :drawing %>
<%= label_tag :drawing, 'Attach drawing here' %>
</div>
<%= submit_tag "Create order", :disable_with => "Creating..." %>
orders_controller.rb
def create
#binding.pry
@order = Order.new#(params[:order])
@order.quantity = params[:quantity_field]
@order.user_id = current_user.id
@order.drawing = params[:order][:drawing]
did_order_save = @order.save
....
当我尝试新订单时,我得到:
Started POST "/orders?multipart=true" for 127.0.0.1 at 2013-02-05 20:07:20 -0800
Processing by OrdersController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"eTInEukPQ84uIw3TPIdI+u0zo49dQlY68gxrodIoPdA=", "quantity_field"=>"334", "order"=>{"drawing"=>"profileBox.png"}, "supplier_list"=>["1"], "commit"=>"Create order", "multipart"=>"true"}
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."remember_token" = 'kGRbFvLaYseFaHvRR6sy6w' LIMIT 1
Completed 500 Internal Server Error in 19882ms
Paperclip::AdapterRegistry::NoHandlerError (No handler found for "profileBox.png"):
app/controllers/orders_controller.rb:54:in `create'
我安装了paperclip / imagemagick,已完成迁移并确认drawing_file_name等在数据库中,并且从那时起已重新启动服务器。