[英]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.