2016-06-13 73 views
0

我有一个Rails 4应用程序,它可以擦除给定的网页并将数据保存在Link模型中。模型不保存Paperclip头像数据

我使用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 
+0

你能向我们展示'controller'和'view' –

+0

我已经添加了LinksController的细节,但是你可以看到它是所有非常标准的东西。我没有任何麻烦,在保存链接后,我无法保存链接数据,并且在Link模型中保存数据。 – BillyBib

+0

此外,我目前没有使用数据在视图 – BillyBib

回答

0

我解决了它。

Link没有得到更新的avatar信息,因为数据没有被任何保存操作保存。所以如果我这样做:

def picture_from_url 
    self.avatar = URI.parse(self.image_url) 
    self.save 
end 

一切工作很好。对于在我的模型中调用self.save可以获得一些反馈。它闻到了我的味道。