views:

366

answers:

2

I used paper clip in my web application, I use this to make a new product:

<% 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 %>

And this to show product:

<% 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 %>

And his is my 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

But after I upload the photo, it show my image path like this :

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

It seems that it can't upload the photo with this error:

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

{:method=>:get}

+2  A: 

I am not sure what semantic_form_for is, but with regular form_for, you have to explicitly say it's a multipart form.

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

If you don't do this, the contents of your file upload fields won't be transmitted to the server. It's a HTML thing ;)

August Lilleaas
I seee, I am missing the :html => {:multipart => true}, wt is it for??html => {:multipart => true}
Ted Wong
Updated my post ;)
August Lilleaas
A: 

I think you paste the image into public/images folder in your rails application.And give the path like in your browser "/images/missing.png".

And your index will be <%= image_tag @product.photo%>