2014-09-24 25 views
0

我想只將縮略圖圖像作爲二進制數據存儲在數據庫中。
到目前爲止,我使用carrierwave將圖像存儲在文件系統中。它可以保存原始文件和大拇指。Rails4:如何在數據庫中只存儲嵌套模型的縮略圖?

如何更改我的資源以僅在數據庫中存儲縮略圖?
我使用關聯。 article有許多photo

\ uploders \ image_uploder.rb

class ImageUploader < CarrierWave::Uploader::Base 
. 
. 
    include CarrierWave::RMagick 
    storage :file 
    def store_dir 
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}" 
    end 
. 
. 
end 

文章表

sqlite> .schema articles 
CREATE TABLE "articles" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "content" varchar(255),"user_id" integer, "created_at" datetime, "updated_at" datetime, "category_id" integer); 

照片表

sqlite> .schema photos 
CREATE TABLE "photos" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "article_id" integer,"image" varchar(255), "created_at" datetime, "updated_at" datetime, "bin_image" blob); #prepare bin_image for saving image as binary 

\型號\ article.rb

class Article < ActiveRecord::Base 
    belongs_to :user 
. 
. 
    has_many :photos, dependent: :destroy 
    accepts_nested_attributes_for :photos, reject_if: proc { |attributes| attributes[:image].blank? }, allow_destroy: true 
    validate :check_for_at_least_image 
. 
. 
    def build_images 
     (3 - self.photos.size).times {self.photos.build} 
    end 

    def check_for_at_least_image 
     errors.add(:image, "select...") if photos.reject(&:marked_for_destruction?).size <= 0 
    end 
. 
. 
end 

\模型\ photo.rb

class Photo < ActiveRecord::Base 
    belongs_to :article 
    mount_uploader :image, ImageUploader 
end 

\控制器\ articles_controller.rb

class ArticlesController < ApplicationController 

    before_action :signed_in_user, only: [:create, :destroy] 
    before_action :correct_user, only: [:update, :destroy] 
. 
. 
    def new 
    @article = Article.new 
    @category = Category.find(params[:category]) 
    @article.category_id = @category.id 
    3.times { @article.photos.build } 
    end 

    def create 
    @article = current_user.articles.build(article_params) 
    if @article.save 
     flash[:success] = "article created!" 
     redirect_to current_user #root_url 
    else 
     @article.build_images 
     render 'new' 
    end 
    end 

    def edit 
    @article = Article.find(params[:id]) 
    @article.build_images 
    end 

    def update 
    @article = Article.find(params[:id]) 
    if @article.update_attributes(article_params) 
     redirect_to current_user 
    else 
     render 'edit' 
    end 
    end 
. 
. 
    private 

    def article_params 
     params.require(:article).permit(:content, :category_id, photos_attributes: [:id, :article_id, :image, :image_cache, :_destroy]) 
    end 
. 
. 
end 

\視圖\共享\ _article_form.html.erb

. 
. 
<%= form_for(@article) do |f| %> 
    <%= render 'shared/error_messages', object: f.object %> 
    <div class="field"> 
    <%= f.hidden_field :category_id %> 
    <%= f.fields_for :photos do |p| %> 
     <%= p.hidden_field :article_id %> 
     <div class="photo"> 
     <% if p.object.image and p.object.image.file %> 
     <%= image_tag p.object.image.thumb.url %> 
     <%= p.hidden_field :image_cache if p.object.image_cache %> 
     <label><%= p.check_box :_destroy %>&nbsp;delete</label> 
     <% end %> 
     <%= p.file_field :image %> 
     </div> 
    <% end %> 
    <%= f.text_area :content, placeholder: "enter content..." %> 
    </div> 
    <%= f.submit class: "btn btn-large btn-primary" %> 
<% end %> 

回答