簡體   English   中英

模型未保存回形針頭像數據

[英]Model not saving Paperclip avatar data

我有一個Rails 4應用程序,它可以抓取給定的網頁並將數據保存在Link模型中。

我將Paperclip gem與after_save回調結合使用,以獲取圖像的url,然后將其上傳到AWS S3。 一切正常,我可以在S3存儲桶中查看圖像。

我的問題是數據庫中的avatar_file_nameavatar_content_typeavatar_file_sizeavatar_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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM