【问题标题】:Can't upload photo using paper clip无法使用回形针上传照片
【发布时间】:2011-01-03 11:22:51
【问题描述】:

我在我的网络应用程序中使用了回形针,我用它来制作一个新产品:

<% semantic_form_for @product do |f| %>  
  <% f.inputs do %>  
    <%= f.input :title %>  
    <%= f.input :price %>  
    <%= f.file_field :photo %>


    <%= f.input :category , :include_blank => false %>  
  <% end %>  
  <%= f.buttons %>  
<% end %>

这是展示产品:

<% semantic_form_for @product do |f| %>  
<%= image_tag @product.photo.url%>

  <% f.inputs do %>  
    <%= f.input :title %>  
    <%= f.input :price %>  
    <%= f.file_field :photo %>

    <%= f.input :category , :include_blank => false %>  
  <% end %>  
  <%= f.buttons %>  
<% end %>

他是我的product.rb:

class Product < ActiveRecord::Base
  validates_presence_of :title, :price
  validates_numericality_of :price
  validates_uniqueness_of :title

  has_attached_file :photo
  attr_accessible :name, :category_id, :price, :title, :photo
  belongs_to :category
  has_many :order_items

end

但是在我上传照片后,它会显示我的图像路径:

http://localhost:3000/photos/original/missing.png

似乎无法上传照片,出现此错误:

No route matches "/photos/original/missing.png" with

{:method=>:get}

【问题讨论】:

    标签: ruby-on-rails ruby upload file-upload


    【解决方案1】:

    我不确定semantic_form_for 是什么,但是对于常规的form_for,你必须明确地说它是一个多部分表单。

    <% form_for(@thing, :html => {:multipart => true}) do |f| %>
      ....
    

    如果您不这样做,您的文件上传字段的内容将不会传输到服务器。这是一个 HTML 东西 ;)

    【讨论】:

    • 我明白了,我错过了:html => {:multipart => true},它是为了什么? html => {:multipart => true}
    【解决方案2】:

    我认为您将图像粘贴到您的 rails 应用程序中的 public/images 文件夹中。并在浏览器中提供路径“/images/missing.png”。

    你的索引将是

    【讨论】:

      猜你喜欢
      • 2011-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-06
      • 1970-01-01
      • 2014-08-24
      • 2013-11-09
      相关资源
      最近更新 更多