undefined method `url' for “#<ActionDispatch::Http::UploadedFile:0x007f8b6134d610>”:String

岁酱吖の 提交于 2020-02-22 08:09:03

问题


I am using Carrierwave to upload picture, after uploaded, I got the error when go to show page:

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

Here is my code:

_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 %>

Anyone know how to solve the problem?

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

Here is the version in Gemfile:

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

回答1:


I would check first strong params and add :picture attribute if it hasn't been added.

Then I would try to add this in your show views:

show.html.erb

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

instead of this line of code:

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

In case that you have permissions problem, you can create config file:

config/initializers/carrierwave.rb

and add permissions:

CarrierWave.configure do |config|
  config.permissions = 0666
  config.directory_permissions = 0777
  config.storage = :file
end


来源:https://stackoverflow.com/questions/41321416/undefined-method-url-for-actiondispatchhttpuploadedfile0x007f8b6134d61

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!