2013-07-24 21 views
0

so paperclip,似乎有一種不同的方式來讓它每次我使用它的工作。表單不保存,如果嘗試上傳圖像與回形針

所以目前我嘗試提交表單,但失敗並重新呈現表單(如果表單沒有保存,這是它應該做的)。

這是我的設置到目前爲止

的Gemfile

gem "paperclip", "~> 3.0" 

控制器

def new 
    @post = Post.new 
end 

def create 
@user = current_user 
@post = @user.posts.new(params[:post]) 
    if @post.save 
    redirect_to root_path, :notice => 'Post Successfully Created' 
    else 
    render :action => 'new' 
    end 
end 

郵政型號

class Post < ActiveRecord::Base 
    belongs_to :category 
    belongs_to :user 

attr_accessible :comments, :title, :category_id, :user_id, :photo 
has_attached_file :photo, :styles => { :medium => "300x300>", :thumb => "100x100>" } 
end 

形式

<%= form_for @post, :class => 'post-form', :html => { :multipart => true } do |f| %> 

<%= f.label :title, "Title", :class => 'title_label' %> 
<%= f.text_field :title %> 

<%= f.label :category_id, "Choose Category", :class => 'title_label' %> 
<%= f.collection_select(:category_id, Category.all, :id, :name, :prompt => "Please Select a Category") %> 

<%= f.label :comments, "Comments", :class => 'title_label' %> 
<%= f.text_area :comments %><br> 

<%= f.file_field :photo %> 

<%= f.submit 'Submit', :class => 'btn' %> 

<% end %> 

我的遷移添加照片是成功的,因爲我的架構看起來像這樣

create_table "posts", :force => true do |t| 
t.string "title" 
t.text  "comments" 
t.integer "category_id" 
t.integer "user_id" 
t.datetime "created_at",   :null => false 
t.datetime "updated_at",   :null => false 
t.string "photo_file_name" 
t.string "photo_content_type" 
t.integer "photo_file_size" 
t.datetime "photo_updated_at" 

任何人都可以看到原因,如預期,這是不工作?

編輯

我是否需要安裝ImageMagick的,讓圖像的上傳或者這只是在視圖中呈現的圖像?

確定,所以從我已經開始嘗試和調試,並把這個在我看來

<%= @post.errors.full_messages %> 

的意見,我得到這個返回

["Photo C:/Users/RICHAR~1/AppData/Local/Temp/bitman20130724-5600-agvtgn.png is not recognized by the 'identify' command.", "Photo C:/Users/RICHAR~1/AppData/Local/Temp/bitman20130724-5600-agvtgn.png is not recognized by the 'identify' command."] 

任何想法?

感謝

+0

你使用Rails 3.x版本嗎?嘗試打印表單錯誤:debug @ post.errors – Johny

+0

這是否在我的控制器?在創造行動? – Richlewis

+0

你可以在瀏覽器中使用它,'<%= @ post.errors%>'來查看它。 – rmagnum2002

回答

1

步驟1 從回形針文檔:

ImageMagick的必須安裝回形針必須訪問它。要確保它在命令行上運行哪個轉換(ImageMagick實用程序之一)。這將爲您提供安裝該實用程序的路徑。例如,它可能會返回/usr/local/bin/convert

然後,在您的環境配置文件中,讓Paperclip知道通過將該目錄添加到其路徑中。

development模式下,你可能會加入這一行config/environments/development.rb

Paperclip.options[:command_path] = "/usr/local/bin/" 

步驟2 對於agvtgn.png is not recognized by the 'identify' command.錯誤:

不知道你是如何在windows做到這一點,對於Linux這是你需要做的事情:

$ which identify 
/path/to/identify 

套裝command_path到路徑config/environments/development.rb

Paperclip.options[:command_path] = "/path/to" 

還需要ImageMagick被安裝

http://ganeshprasadsr.blogspot.com/2010/12/paperclip-issue-is-not-recognized-by.html


我覺得 - 你只需要安裝ImageMagick的。


p.s. Windows是最糟糕的開發機器。您至少可以安裝一臺運行在Linux上的虛擬機。

+0

約定的窗口是最糟糕的開發機器.... EVER..however所有我在日子裏訪問(在家裏使用Linux),VM不可能,相信我如果這是我將使用它....我添加Paperclip.options,現在似乎工作,雖然我確定路徑不是/ usr/local/bin,但它的所有工作到目前爲止 – Richlewis

+0

'/ usr/local/bin'這個路徑si只是它的一個例子。祝你的發展順利;)。 – rmagnum2002