参考 CarrierWave 的官方文档,但图像不显示
Referred CarrierWave's official documentation but image not displaying
我正在 Rails Ruby 中开发一个项目,我必须为其上传一张图片。为此,我遇到了 CarrierWave,因此我参考了它的官方文档。但是,当我上传图片时,图片会存储在数据库中,但是当我尝试使用 <%= image_tag @books.cover.url %>
显示它时,出现以下错误:
undefined method `url' for "#ActionDispatch::Http::UploadedFile:0x000000000e983c68>":String
请找到以下文件的代码:
app/views/books/_form.html.erb
<%= form_with(model: book, local: true) do |form| %>
<% if book.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(book.errors.count, "error") %> prohibited this book from being saved:</h2>
<ul>
<% book.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= form.label :cover %>
<%= form.file_field :cover %>
</div>
<div class="actions">
<%= form.submit %>
</div>
<% end %>
app/models/book.rb
class Book < ApplicationRecord
belongs_to :library
mount_uploader :image, ImageUploader
end
app/views/books/show.html.erb
<p>
<strong>Cover:</strong>
<%= image_tag @book.cover.url if @book.cover? %>
</p>
app/uploaders/image_uploader.rb
# encoding: utf-8
class ImageUploader < 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(jpeg 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
# Upload Images
gem 'carrierwave'
gem 'mini_magick'
Ruby版本:2.6.4
Rails版本:6.0.0
我也在 Whosebug 上阅读了一些问题,例如 this,但没有适合我的解决方案。我什至尝试阅读一些有关 CarrierWave 的在线教程,但它们使用的代码与我使用的代码相同,但这些代码适用于它们,但不适用于我。
如果有人以前遇到过这个问题,或者您需要更多信息,请告诉我。
我想你想为 :cover
安装上传器。
class Book < ApplicationRecord
belongs_to :library
mount_uploader :cover, ImageUploader
end
我正在 Rails Ruby 中开发一个项目,我必须为其上传一张图片。为此,我遇到了 CarrierWave,因此我参考了它的官方文档。但是,当我上传图片时,图片会存储在数据库中,但是当我尝试使用 <%= image_tag @books.cover.url %>
显示它时,出现以下错误:
undefined method `url' for "#ActionDispatch::Http::UploadedFile:0x000000000e983c68>":String
请找到以下文件的代码:
app/views/books/_form.html.erb
<%= form_with(model: book, local: true) do |form| %>
<% if book.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(book.errors.count, "error") %> prohibited this book from being saved:</h2>
<ul>
<% book.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= form.label :cover %>
<%= form.file_field :cover %>
</div>
<div class="actions">
<%= form.submit %>
</div>
<% end %>
app/models/book.rb
class Book < ApplicationRecord
belongs_to :library
mount_uploader :image, ImageUploader
end
app/views/books/show.html.erb
<p>
<strong>Cover:</strong>
<%= image_tag @book.cover.url if @book.cover? %>
</p>
app/uploaders/image_uploader.rb
# encoding: utf-8
class ImageUploader < 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(jpeg 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
# Upload Images
gem 'carrierwave'
gem 'mini_magick'
Ruby版本:2.6.4
Rails版本:6.0.0
我也在 Whosebug 上阅读了一些问题,例如 this,但没有适合我的解决方案。我什至尝试阅读一些有关 CarrierWave 的在线教程,但它们使用的代码与我使用的代码相同,但这些代码适用于它们,但不适用于我。 如果有人以前遇到过这个问题,或者您需要更多信息,请告诉我。
我想你想为 :cover
安装上传器。
class Book < ApplicationRecord
belongs_to :library
mount_uploader :cover, ImageUploader
end