【问题标题】:undefined method `url' for "#<ActionDispatch::Http::UploadedFile:0x007f8b6134d610>":String“#<ActionDispatch::Http::UploadedFile:0x007f8b6134d610>”的未定义方法“url”:字符串
【发布时间】:2020-07-19 16:57:50
【问题描述】:

我正在使用Carrierwave上传图片,上传后进入显示页面时出现错误:

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>

产品.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 %>

有人知道如何解决这个问题吗?

更新:

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'

【问题讨论】:

  • 你能显示PictureUploader吗?
  • 据了解,图片上传了吗?您在“公共/上传”文件夹中看到它了吗?并尝试在 enviroment.rb 中设置它 - require 'carrierwave/orm/activerecord'

标签: ruby-on-rails carrierwave


【解决方案1】:

我会检查第一个强参数并添加:picture 属性,如果它没有被添加的话。

然后我会尝试在你的节目视图中添加这个:

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

【讨论】:

    【解决方案2】:

    在模型中,你需要添加

    块引用 mount_uploader :图片,图片上传 块引用

    【讨论】:

    • 它已经存在了 - 请参阅他们发布的 product.rb
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-14
    相关资源
    最近更新 更多