2016-12-25 3 views
0

Я использую Carrierwave, чтобы загрузить изображение, после закачано, я получил ошибку, когда идут, чтобы показать страницу:неопределенный метод `URL» для "# <ActionDispatch :: Http :: UploadedFile: 0x007f8b6134d610>": String

undefined method `url' for "#ActionDispatch::Http::UploadedFile:0x007f8b6134d610>":String 
<%= image_tag @product.picture.url if @product.picture? %> 

Вот мой код:

_form.html.erb

<div class="picture"> 
    <%= f.file_field :picture %> 
    </div> 

product.rb

class Product < ApplicationRecord 
    has_many :reviews, dependent: :destroy 
    mount_uploader :picture, PictureUploader 
end 

show.html.erb

<p id="notice"><%= notice %></p> 
<p> 
    <strong>Name:</strong> 
    <%= @product.name %> 
</p> 
    <strong>Picture:</strong> 
    <%= image_tag @product.picture.url if @product.picture? %> 
</p> 
<%= link_to 'Edit', edit_product_path(@product) %> | 
<%= link_to 'Back', products_path %> 

Каждый знает, как решить эту проблему?

Update:

picture_uploader.rb

# encoding: utf-8 

class PictureUploader < 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 

    # Provide a default URL as a default if there hasn't been a file uploaded: 
    # def default_url 
    # # For Rails 3.1+ asset pipeline compatibility: 
    # # ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_')) 
    # 
    # "/images/fallback/" + [version_name, "default.png"].compact.join('_') 
    # end 

    # Process files as they are uploaded: 
    # process :scale => [200, 300] 
    # 
    # def scale(width, height) 
    # # do something 
    # end 

    # Create different versions of your uploaded files: 
    # version :thumb do 
    # process :resize_to_fit => [50, 50] 
    # end 

    # Add a white list of extensions which are allowed to be uploaded. 
    # For images you might use something like this: 
    # def extension_white_list 
    # %w(jpg jpeg gif png) 
    # end 

    # Override the filename of the uploaded files: 
    # Avoid using model.id or version_name here, see uploader/store.rb for details. 
    # def filename 
    # "something.jpg" if original_filename 
    # end 

end 

Вот версия в Gemfile:

gem 'carrierwave',    '0.11.2' 
gem 'mini_magick',    '4.5.1' 
gem 'fog',      '1.38.0' 
+0

вы можете показать 'PictureUploader'? – emaillenin

+0

как понял, изображение загружается? Вы видите это в своей папке «public/uploads»? И попробуйте установить это в enviroment.rb - 'require 'carrierwave/orm/activerecord'' –

ответ

0

Я хотел бы проверить первые сильные Титулы и добавить :picture атрибут, если он hasn» t.

Тогда я хотел бы попробовать добавить это в ваших взглядах шоу:

show.html.erb

<%= image_tag(@product.picture_url.to_s) %> 

вместо этой строки кода:

<%= image_tag @product.picture.url if @product.picture? %> 

В случае, если вам есть проблема с разрешениями, вы можете создать файл конфигурации:

config/initializers/carrierwave.rb 

и добавить разрешения:

CarrierWave.configure do |config| 
    config.permissions = 0666 
    config.directory_permissions = 0777 
    config.storage = :file 
end 
Смежные вопросы