简体   繁体   中英

Carrierwave does not create upload folder

My main problem is, that when uploading via carrierwave the file isn't truly uploaded.

My console output:

#<OptionPic id: 4, image_url: "game_of_thrones___tyrion_lannister_by_stanbos-d79k...", created_at: "2015-08-07 06:08:01", updated_at: "2015-08-07 06:08:01", option_id: 12>]>

The call to the image is following

-@product.options.each do |option|
          -option.option_pics.each do |op|
            =image_tag op.image_url.to_s

If I inspect the element on my webpage, this is what I get:

<img src="/images/game_of_thrones___tyrion_lannister_by_stanbos-d79k0u9_modified.jpg" alt="Game of thrones tyrion lannister by stanbos d79k0u9 modified">

However settings on the uploader are as following:

class ProductImageUploader < CarrierWave::Uploader::Base

# Include RMagick or MiniMagick support:
include CarrierWave::RMagick
# include CarrierWave::MiniMagick

# Choose what kind of storage to use for this uploader:
storage :file
# storage :fog

# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be    mounted:
def store_dir
   "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end

Needless to say I have neither an image in the assets/images folder nor a public/uploads folder created.

This works perfectly on my other project (public/uploads folder is created with the same syntax as above), where images are stored as a separate model. In my current project I want them as a sub-option for a product class.

The structure is following:

models - product, option, optionpic(ture)

A product can have many options and in turn an option can have many pictures. For example - product is a T-Shirt. Options are: material and color. For each color I want a different picture of the shirt.

This is why I have everything in my ProductsController:

  Class ProductsController < ApplicationController

  before_action :find_product, only: [:show]

  def show
  end

  def index
    @products = Product.all
  end

  def new
    @product = Product.new
    @option = @product.options.new
    @option_pic = OptionPic.new
  end

  def edit
  end

  def create
    @product = Product.create(product_params)
    @option = @product.options.create(option_params)
    @option.product_id = @product.id
    @option.save

    @option_pic = @option.option_pics.create(pic_params)
    @option_pic.option_id = @option.id

    flash[:success] = "Product successfully created!"
    redirect_to products_path
  end

  private

    def product_params
        params.require(:product).permit(:title, :description, :advertising_text, :fancy_quote)
    end

    def option_params
      params.require(:option).permit(:size, :weight, :price, :material, :product_id)
    end

    def pic_params
      params.require(:option_pic).permit(:image_url, :option_id)
    end

    def find_product
        @product = Product.find(params[:id])
    end
end

My optionpic(ture) model:

class OptionPic < ActiveRecord::Base
    mount_uploader :product_image, ProductImageUploader
    belongs_to :option
end

I think I am making just some dumb newbie mistake, but after hours of search I just can't figure out what is wrong.

Edit

I figured out two things - I used image_url instead of picture_image and forgot to actually save the option_picture values in the controller, ie @option_pic.save

How ever now there is no data transmission when uploading:

SQL (0.3ms)  INSERT INTO "option_pics" ("product_image", "option_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id"  [["product_image", nil], ["option_id", 18], ["created_at", "2015-08-07 08:03:28.559319"], ["updated_at", "2015-08-07 08:03:28.559319"]] 

I updated every value from :image_url to :product_image (in the view, in the pic_params etc.)

Edit 2

As requested, my upload form:

%h1 Create new product

=form_for @product, url: products_path do |f|
    %p
        =f.label :title
        =f.text_field :title
        %br
        =f.label :description
        =f.text_area :description
        %br
        =f.label :advertising_text
        =f.text_area :advertising_text
        %br
        =f.label :fancy_quote
        =f.text_area :fancy_quote

    %p
        = fields_for @option do |o|
            =o.label :price
            =o.text_field :price
            %br
            =o.label :size
            =o.text_field :size
            %br
            =o.label :weight
            =o.text_field :weight
            %br
            =o.label :material
            =o.text_field :material
    %p
        = fields_for @option_pic, html: { multipart: true } do |op|
            = op.label 'Upload image'
            = op.file_field :product_image
    =f.submit

Edit 3

As requested, my log parameters

Started POST "/products" for ::1 at 2015-08-07 12:25:31 +0300
Processing by ProductsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"DOeZMvYpdoVmZRpsGmg2Gr9LIc9RYaS1KT1vdfhXI2BJaV3pPZZbZN8PJnvwQAig8wLPpIUORuf7Kjcw3BE6Zg==", "product"=>{"title"=>"fafa", "description"=>"", "advertising_text"=>"", "fancy_quote"=>""}, "option"=>{"price"=>"", "size"=>"", "weight"=>"", "material"=>""}, "option_pic"=>{"product_image"=>"level-3-on-rails-for-zombies-2-0eaaf0109f83459c5aedef30bdf8bd96.png"}, "commit"=>"Create Product"}
   (0.1ms)  BEGIN
  SQL (0.3ms)  INSERT INTO "products" ("title", "description", "advertising_text", "fancy_quote", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id"  [["title", "fafa"], ["description", ""], ["advertising_text", ""], ["fancy_quote", ""], ["created_at", "2015-08-07 09:25:31.365739"], ["updated_at", "2015-08-07 09:25:31.365739"]]
   (0.3ms)  COMMIT
   (0.1ms)  BEGIN
  SQL (0.2ms)  INSERT INTO "options" ("size", "material", "product_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id"  [["size", ""], ["material", ""], ["product_id", 29], ["created_at", "2015-08-07 09:25:31.369112"], ["updated_at", "2015-08-07 09:25:31.369112"]]
   (0.3ms)  COMMIT
   (0.1ms)  BEGIN
   (0.1ms)  COMMIT
   (0.1ms)  BEGIN
  SQL (0.3ms)  INSERT INTO "option_pics" ("product_image", "option_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id"  [["product_image", nil], ["option_id", 21], ["created_at", "2015-08-07 09:25:31.372492"], ["updated_at", "2015-08-07 09:25:31.372492"]]
   (0.3ms)  COMMIT
Redirected to http://localhost:3000/products
Completed 302 Found in 10ms (ActiveRecord: 2.1ms)

You are not accessing your params value properly. The following will fix that:

def pic_params
  params.require(:option_pic).require(:product_image)
end

Also, you have to make your OptionPic belonging to your Option when you build it:

def new
  @product = Product.new
  @option = @product.options.new
  @option_pic = @option.option_pics.new
end

If someone encounters same problem, I will post my solution - I decided to recreate this system using nested attributes, now everything work fine!

So here I have my 3 models:

class Product < ActiveRecord::Base

    belongs_to  :category 
    belongs_to  :subcategory
    has_many    :options

    has_and_belongs_to_many :product_sizes

    accepts_nested_attributes_for :options

end

The option class, which is the child of a product

class Option < ActiveRecord::Base
    belongs_to  :product
    has_many    :option_pics

    accepts_nested_attributes_for :option_pics
end

And finally the option_pic, a child of an option

class OptionPic < ActiveRecord::Base
    mount_uploader :product_image, ProductImageUploader
    belongs_to :option
end

In the products controller my new and create actions look as following:

  def new
    @product = Product.new
    option = @product.options.build
    option_pic = option.option_pics.build
  end

  def edit
  end

  def create
    @product = Product.new(product_params)

    if @product.save!
      flash[:success] = "Product successfully created!"
      redirect_to products_path
    end
  end

  private

    def product_params
        params.require(:product).permit(
        :title, :description, :advertising_text, :fancy_quote, :category_id, 
        options_attributes: [:size, :weight, :price, :material, :product_id, 
          option_pics_attributes: [:product_image, :option_id]])
end

def find_product
    @product = Product.find(params[:id])
end

The string params are double nested, which is important. Also important is the structure for the form_for helper. I did not refactor it, so right now it is one big list. The structure there is very important, I had my fields for option_pic intended on the level of the "f" variable, which was crucial and no images were uploaded.

=form_for @product, url: products_path do |f|
    %p
        =f.label :title
        =f.text_field :title
        %br
        =f.label :description
        =f.text_area :description
        %br
        =f.label :advertising_text
        =f.text_area :advertising_text
        %br
        =f.label :fancy_quote
        =f.text_area :fancy_quote
        %br
        =f.label :category_id
        =f.collection_select :category_id, Category.all, :id, :title, { prompt: 'Please select category' }
        %br
        =f.label 'Product Size'
        =f.collection_check_boxes(:product_size_ids, ProductSize.all, :id, :size)

    %p
        = f.fields_for :options do |builder|
            =builder.label :price
            =builder.text_field :price
            %br
            =builder.label :size
            =builder.text_field :size
            %br
            =builder.label :weight
            =builder.text_field :weight
            %br
            =builder.label :material
            =builder.text_field :material
            %br
            =builder.fields_for :option_pics do |op|
                = op.label 'Upload image'
                = op.file_field :product_image
    =f.submit

And voila, a wonderful commit:

(0.1ms)  BEGIN
  SQL (0.4ms)  INSERT INTO "products" ("title", "description", "advertising_text", "fancy_quote", "category_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING "id"  [["title", "builder"], ["description", ""], ["advertising_text", ""], ["fancy_quote", ""], ["category_id", 1], ["created_at", "2015-08-11 11:34:11.618298"], ["updated_at", "2015-08-11 11:34:11.618298"]]
  SQL (0.2ms)  INSERT INTO "options" ("size", "price", "material", "product_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id"  [["size", ""], ["price", 555], ["material", ""], ["product_id", 82], ["created_at", "2015-08-11 11:34:11.619678"], ["updated_at", "2015-08-11 11:34:11.619678"]]
  SQL (0.5ms)  INSERT INTO "option_pics" ("product_image", "option_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id"  [["product_image", "b3c839_bd43d840e031469495137fa74e31faf4.jpg_srz_428_428_75_22_0.5_1.2_75_jpg_srz"], ["option_id", 66], ["created_at", "2015-08-11 11:34:11.620837"], ["updated_at", "2015-08-11 11:34:11.620837"]]
   (0.3ms)  COMMIT

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM