Ruby on rails 3 CarrierWave显示图像\u url错误
我正在使用CarrierWave将图像上载到我的服务器。然而,当我用image\u url引用它们时,我得到了nil的NoMethodError:NilClass,用于Ruby on rails 3 CarrierWave显示图像\u url错误,ruby-on-rails-3,Ruby On Rails 3,我正在使用CarrierWave将图像上载到我的服务器。然而,当我用image\u url引用它们时,我得到了nil的NoMethodError:NilClass,用于image\u url app/assets/views/albums/index.html.erb <% @albums.each do |album| %> .... <div> <%= image_tag @album.image_url.to_s %> </div>
image\u url
app/assets/views/albums/index.html.erb
<% @albums.each do |album| %>
....
<div>
<%= image_tag @album.image_url.to_s %>
</div>
<%end%>
namespace :admin do
root :to => "dashboard#index"
resources :dashboard
resources :albums do
resources :images
end
get "admin/album"
<div>
<%= f.file_field :image %>
</div>
class Image < ActiveRecord::Base
attr_accessible :album_id, :title, :image
belongs_to :album
mount_uploader :image, ImageUploader
end
# encoding: utf-8
class ImageUploader < CarrierWave::Uploader::Base
# Include RMagick or MiniMagick support:
# include CarrierWave::RMagick
# include CarrierWave::MiniMagick
# Include the Sprockets helpers for Rails 3.1+ asset pipeline compatibility:
# include Sprockets::Helpers::RailsHelper
# include Sprockets::Helpers::IsolatedHelper
# 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:
# # 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 :scale => [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
<h1>These are the albums</h1>
<% @albums.each do |album| %>
<div class="item" style="background: #e4e4e4; border: 2px solid #c9c9c9; padding:1.5em 0 1.5em 0; width:17em; font-family: 'Signika Negative';">
<div class="title" style="background: #d3d3d3; display: inline-block; padding:1.5em 0 1.5em 1em; width: 12em;">
<strong>Title</strong> </br>
<%= album.title %></div>
</br>
<div class="description" style="background: #f9f9f9; display: inline-block; padding: 1.5em 0 1.5em 1em; width: 12em;">
<strong>Description</strong> </br>
<%= album.description %>
</div>
</br>
<div>
<%= image_tag album.image.image_url.to_s %>
</div>
<div>
</div>
<%= link_to "Delete", admin_album_path(album), :method => :delete, :style => "margin: 1em 0 0 1em; display: block;" %>
</br>
</div>
<% end %>
<%=link_to "New Album", :controller => "albums", :action => "new" %>
<%=link_to "Manage Pictures"%>
app/assets/views/albums/new.html.erb
<% @albums.each do |album| %>
....
<div>
<%= image_tag @album.image_url.to_s %>
</div>
<%end%>
namespace :admin do
root :to => "dashboard#index"
resources :dashboard
resources :albums do
resources :images
end
get "admin/album"
<div>
<%= f.file_field :image %>
</div>
class Image < ActiveRecord::Base
attr_accessible :album_id, :title, :image
belongs_to :album
mount_uploader :image, ImageUploader
end
# encoding: utf-8
class ImageUploader < CarrierWave::Uploader::Base
# Include RMagick or MiniMagick support:
# include CarrierWave::RMagick
# include CarrierWave::MiniMagick
# Include the Sprockets helpers for Rails 3.1+ asset pipeline compatibility:
# include Sprockets::Helpers::RailsHelper
# include Sprockets::Helpers::IsolatedHelper
# 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:
# # 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 :scale => [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
<h1>These are the albums</h1>
<% @albums.each do |album| %>
<div class="item" style="background: #e4e4e4; border: 2px solid #c9c9c9; padding:1.5em 0 1.5em 0; width:17em; font-family: 'Signika Negative';">
<div class="title" style="background: #d3d3d3; display: inline-block; padding:1.5em 0 1.5em 1em; width: 12em;">
<strong>Title</strong> </br>
<%= album.title %></div>
</br>
<div class="description" style="background: #f9f9f9; display: inline-block; padding: 1.5em 0 1.5em 1em; width: 12em;">
<strong>Description</strong> </br>
<%= album.description %>
</div>
</br>
<div>
<%= image_tag album.image.image_url.to_s %>
</div>
<div>
</div>
<%= link_to "Delete", admin_album_path(album), :method => :delete, :style => "margin: 1em 0 0 1em; display: block;" %>
</br>
</div>
<% end %>
<%=link_to "New Album", :controller => "albums", :action => "new" %>
<%=link_to "Manage Pictures"%>
app/assets/views/albums/index.html.erb
<% @albums.each do |album| %>
....
<div>
<%= image_tag @album.image_url.to_s %>
</div>
<%end%>
namespace :admin do
root :to => "dashboard#index"
resources :dashboard
resources :albums do
resources :images
end
get "admin/album"
<div>
<%= f.file_field :image %>
</div>
class Image < ActiveRecord::Base
attr_accessible :album_id, :title, :image
belongs_to :album
mount_uploader :image, ImageUploader
end
# encoding: utf-8
class ImageUploader < CarrierWave::Uploader::Base
# Include RMagick or MiniMagick support:
# include CarrierWave::RMagick
# include CarrierWave::MiniMagick
# Include the Sprockets helpers for Rails 3.1+ asset pipeline compatibility:
# include Sprockets::Helpers::RailsHelper
# include Sprockets::Helpers::IsolatedHelper
# 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:
# # 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 :scale => [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
<h1>These are the albums</h1>
<% @albums.each do |album| %>
<div class="item" style="background: #e4e4e4; border: 2px solid #c9c9c9; padding:1.5em 0 1.5em 0; width:17em; font-family: 'Signika Negative';">
<div class="title" style="background: #d3d3d3; display: inline-block; padding:1.5em 0 1.5em 1em; width: 12em;">
<strong>Title</strong> </br>
<%= album.title %></div>
</br>
<div class="description" style="background: #f9f9f9; display: inline-block; padding: 1.5em 0 1.5em 1em; width: 12em;">
<strong>Description</strong> </br>
<%= album.description %>
</div>
</br>
<div>
<%= image_tag album.image.image_url.to_s %>
</div>
<div>
</div>
<%= link_to "Delete", admin_album_path(album), :method => :delete, :style => "margin: 1em 0 0 1em; display: block;" %>
</br>
</div>
<% end %>
<%=link_to "New Album", :controller => "albums", :action => "new" %>
<%=link_to "Manage Pictures"%>
这些是相册
标题
说明
:delete,:style=>“页边距:1em 0 0 1em;显示:块;”%>
“相册”,:操作=>“新”%>
你似乎与相册和图像有着某种联系。假设相册中有许多您需要的图像:
@albums.each do |album|
album.images.each{|image| image.image_url }
end
它不应该是普通的相册.image.image\u url而不是相册.image\u url还是同样的错误。我试过很多东西。。。我很困惑。你确定相册。图片给你什么,做这个时会出现什么错误吗?是的,我可以确认这是完全相同的错误
nil:NilClass的未定义方法“image\u url”两次都是。什么是album.image
give-uthis清除了错误,但它仍然没有链接到图像,只是/assets
上载的图像实际上是公开/上载的吗?您可能需要查看视图以确保正确上载图像。查看此处以构建用于上载图像的嵌套表单: