我正在尝试让回形针与 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
然而:
- 我在该路径下、我的系统上或应用程序文件中找不到任何内容
从像这样的其他答案来看,我从表单传递的参数哈希似乎应该有更多的内容;现在只是一个字符串名称,看起来太短了
[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"}
订单.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..." %>
订单控制器.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'
我已经安装了回形针/imagemagick,已经完成了迁移并确认了 drawing_file_name 等在数据库中,并且已经重新启动了服务器。