I don't know why I can't display an image when I create a product using CarrierWave? I also get an error now when I want to edit my product.
I'm using Rails -v 4.2.1 & ruby 2.1.5p273 (2014-11-13 revision 48405) [x86_64-darwin14.0]
controllers/products_controller.rb
class ProductsController < ApplicationController
before_filter :find_product, only: [:edit, :show, :update, :destroy]
def index
@products = Product.order("created_at DESC")
end
def show
end
def new
@product = Product.new
end
def create
@product = Product.new(product_params)
if @product.save!
flash[:success] = "Product created!"
redirect_to products_path
else
flash[:danger] = "Error creating product."
redirect_to new_product_path
end
end
def edit
end
def update
if @product.update_attributes(product_params)
flash[:success] = "Product Updated!"
redirect_to products_path
else
redirect_to "edit"
end
end
private
def product_params
params.require(:product).permit(:name, :description, :price, :images)
end
def find_product
@product = Product.find(params[:product_id])
end
end
models/product.rb
class Product < ActiveRecord::Base
mount_uploader :image, ImageUploader
end
uploaders/image_uploader.rb
class ImageUploader < CarrierWave::Uploader::Base
include CarrierWave::RMagick
storage :file
def extension_white_list
%w(jpg jpeg gif png)
end
version :thumb do
process resize_to_fill: [200, 200]
end
version :small_thumb, from_version: :thumb do
process resize_to_fill: [20, 20]
end
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
end
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire