使用回形针更新模型
Updating a model with paperclip
我的应用程序的工作流程如下,使用 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
不是一种方法。 Paperclip 生成了 verification_token_file_name
、verification_token_content_type
、verification_token_file_size
、verification_token_updated_at
,所以我不确定应该更新哪个属性。
如何更新模型以上传图片?
您还需要在模型中定义附件(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
我的应用程序的工作流程如下,使用 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
不是一种方法。 Paperclip 生成了 verification_token_file_name
、verification_token_content_type
、verification_token_file_size
、verification_token_updated_at
,所以我不确定应该更新哪个属性。
如何更新模型以上传图片?
您还需要在模型中定义附件(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