carrierwaveuploader / carrierwave

Classier solution for file uploads for Rails, Sinatra and other Ruby web frameworks
https://github.com/carrierwaveuploader/carrierwave
8.78k stars 1.66k forks source link

Carrierwave not saving the @storage and other necessary items. #1705

Closed uchennafokoye closed 9 years ago

uchennafokoye commented 9 years ago

I am using ActiveAdmin and Single Table Inheritance. I have a model "Simple Portfolio Model" that a variety of models that have similar portfolio fields inherit from. Also, I have a model "Asset" that image and video model inherit from. A Simple Portfolio Model has_one asset and asset belongs_to SimplePortfolioModel. I was able to create a form that send both the portfolio details and the uploaded assets through parameters and then to an action that will create both STI models. However, after being saved, the file is uploaded, but there are no fields such as url and items such as @storage and others are missing.

Any clues will definitely be appreciated.

ImageUploader:

   class ImageUploader < CarrierWave::Uploader::Base

  include CarrierWave::RMagick
  storage :file

 def store_dir

  if model.portfolio_name
  "uploads/#{model.portfolio_name.underscore}/#{mounted_as}/#{model.id}"
   else 
   "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end 

  end

   ...more code

end

Form:

 <div style="margin: 50px auto; width: 50%;">
  <%= form_for(:example, :url => {:action => 'create'}, :html => {:multipart => true}) do | f| %>

<div class="field">
        <%= f.label :name %><br/>
        <%= f.text_field :name %>
</div>

<div class="field">
        <%= f.label :client %><br/>
        <%= f.select(:client_id, Client.all.collect {|p| [ p.name, p.id ] }, { include_blank: true }) %>
</div>

<div class="field">
        <%= f.label :url %><br/>
        <%= f.url_field :url %>
</div>

<%= f.fields_for :image do | p | %>

    <div class="field">
        <%= p.label :image %><br/>
        <%= p.file_field :image, :multiple=> true, name: "image" %>
    </div>

<% end %>

<br/>

<div class="action">

    <%= f.submit %>
</div>

 <% end %>
 </div

The ActiveAdmin Create Action which dynamically creates Image model which inherits from Asset and calls the ImageUploader functions

 ActiveAdmin.register Example do 
 params.permit .....

 member_action :create, method: :post do

@model = Image.new
@model.portfolio_name = "Example"

uploader = ImageUploader.new(@model, :attachment)
uploader.store!(image_file_params)
@model.save

@example = Example.new(example_params)
if (@example.save)
    @model.simple_portfolio_model_id = @example.id
    @model.save
    redirect_to :action => :show, :id => @example.id
else 
    flash[:error] = "Example couldn't be saved"
    render :new
end  
 end 

end

 class Image < Asset
 mount_uploader :attachment
 end 

class Asset < ActiveRecord::Base
 # Include default devise modules. Others available are:
 # :confirmable, :lockable, :timeoutable and :omniauthable
 belongs_to :simple_portfolio_model

end

It is saving the file, alright, but is missing essential parts such as the url. So when I access the image like this:

   s = Example.first
    s = s.image.attachment

I get:

 <ImageUploader:0x007f85a9a79140 @model=#<Image id: 22, name: "Test", simple_portfolio_model_id: 1, url: "www.yahoo.com", position: nil, type: "Image", created_at: "2015-07-22 21:42:04", updated_at: "2015-07-22 21:42:04", asset: "nobrand-ipad-ilp.mp4", active: nil>, @mounted_as=:attachment>

but then, I try:

   s.image.attachment.url 

and get

    nil 

This is how a carrierwave model should look when you access it (s.image.attachment, for instance)

<ImageUploader:0x007f85a9a79140 @model=#<Image id: 22, name: "Test", simple_portfolio_model_id: 1, url: "www.yahoo.com", position: nil, type: "Image", created_at: "2015-07-22 21:42:04", updated_at: "2015-07-22 21:42:04", asset: "nobrand-ipad-ilp.mp4", active: nil>, @mounted_as=:attachment, @storage=#<CarrierWave::Storage::File:0x007f85a9a78f88 @uploader=#<ImageUploader:0x007f85a9a79140 ...>>, @file=#<CarrierWave::SanitizedFile:0x007f85a9a78650 @file="/Users....", @original_filename=nil, @content_type=nil>, @versions={}>

What am I missing? Thank you.

bensie commented 9 years ago

Please post usage questions to Stack Overflow.