2015-10-20 59 views
0

我的應用程序的工作流程如下,使用angularjs前端應用程序,用戶創建一篇文章,如果成功,則必須提交圖像。用回形針更新模型

我跑rails generate paperclip article verification_token創建以下遷移:

class AddAttachmentVerificationTokenToArticles < ActiveRecord::Migration 
    def self.up 
    change_table :articles do |t| 
     t.attachment :verification_token 
    end 
    end 

    def self.down 
    remove_attachment :articles, :verification_token 
    end 
end 

,並在我的控制器我創建了一個新的動作,send_verification_token

def send_verification_token 
    @article = current_user.articles.find(params[:id]) 
    if @article.update_attribute(:verification_token, params[:file]) 
    render json: @article.id, status: 201 
    else 
    render json: @article.errors, status: 422 
    end 
end 

,但我得到和錯誤,這verification_token不是方法。回形針生成verification_token_file_name,verification_token_content_typeverification_token_file_size,verification_token_updated_at,所以我不知道我應該更新哪個屬性。

如何更新模型以上傳圖片?

回答

1

您還需要定義模型中的附件(has_attached_file):

class Article < ActiveRecord::Base 
    has_attached_file :verification_token 

    validates_attachment_content_type :verification_token, content_type: /\Aimage\/.*\Z/ 
end 

這種方法有很多選項,檢查文檔:當然http://www.rubydoc.info/gems/paperclip/Paperclip/ClassMethods

+0

我是個白癡,那失蹤 – tvieira