[英]Model not saving Paperclip avatar data
我有一个Rails 4应用程序,它可以抓取给定的网页并将数据保存在Link
模型中。
我将Paperclip gem与after_save
回调结合使用,以获取图像的url,然后将其上传到AWS S3。 一切正常,我可以在S3存储桶中查看图像。
我的问题是数据库中的avatar_file_name
, avatar_content_type
, avatar_file_size
和avatar_updated_at
列保持为nil
,好像没有:avatar
附加在Link
link.rb:
class Link < ActiveRecord::Base
after_save :picture_from_url
has_attached_file :avatar, styles: { medium: "300x300>", thumb: "100x100>" }, default_url: "/images/:style/missing.png"
validates_attachment_content_type :avatar, content_type: /\Aimage\/.*\Z/
...
def picture_from_url
self.avatar = URI.parse(self.image_url)
end
end
更新:带有控制器代码
class LinksController < ApplicationController
...
def new
@link = Link.new
end
def create
@link = Link.create(link_params)
if @link.save
flash[:success] = "Thanks for your submission!"
redirect_to link_path(@link)
else
render :new
end
end
private
def link_params
params.require(:link).permit(:title, :url, :content, :tag_list, :category, :image_url, :avatar).merge(user_id: current_user.id, author_first_name: current_user.first_name)
end
end
我解决了。
Link
未获取更新的avatar
信息,因为没有通过任何类型的保存操作来保留数据。 因此,如果我这样做:
def picture_from_url
self.avatar = URI.parse(self.image_url)
self.save
end
一切都很好。 在模型中获得一些有关调用self.save
反馈会很棒。 我闻到了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.