PortfoliosController#index中的NameError未初始化的常量Portfolio :: PortfolioUploader

时间:2018-09-23 18:36:02

标签: ruby-on-rails carrierwave

这是我的文件Portfolio.rb 如果显示为灰色,则我对此代码有疑问:  mount_uploader:thumb_image,PortfolioUploader  mount_uploader:main_image,PortfolioUploader 然后这两行我将使用占位符和投资组合种子以及所有信息访问我的投资组合页面。 当取消标记这两行代码时,我将这篇文章的标题视为错误。我尽一切努力解决了这个问题。没有成功。

  class Portfolio < ApplicationRecord
  has_many :technologies
  accepts_nested_attributes_for :technologies,
                                reject_if: lambda { |attrs| attrs['name'].blank? }

  include Placeholder
  validates_presence_of :title, :body, :main_image, :thumb_image

  mount_uploader :thumb_image, PortfolioUploader
  mount_uploader :main_image, PortfolioUploader

  def self.angular
    where(subtitle: 'Angular')
  end

  def self.by_position
    order("position ASC")
  end

  scope :ruby_on_rails_portfolio_items, -> { where(subtitle: 'Ruby on Rails') }

  after_initialize :set_defaults

  def set_defaults
    self.main_image ||= Placeholder.image_generator(height: '600', width: '400')
    self.thumb_image ||= Placeholder.image_generator(height: '350', width: '200')
  end
end

我代码的另一部分是我的文件_portfolio_item.erb

 <div class="card" data-id="<%= portfolio_item.id %>">
    <%= image_tag portfolio_item.thumb_image unless portfolio_item.thumb_image.nil? %>
  <p class="card-text">
    <span><%= link_to portfolio_item.title, portfolio_show_path(portfolio_item) %></span> <%= portfolio_item.subtitle %>
  </p>
</div>

我的Portfolio_uploader.rb是:

class PortfolioUploader < CarrierWave::Uploader::Base


  storage :file

  def store_dir
  "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end

  def extension_whitelist
    %w(jpg jpeg gif png)
  end

end

我的_portfolio_item_html.erb

<div class="card" data-id="<%= portfolio_item.id %>">
    <%= image_tag portfolio_item.thumb_image unless portfolio_item.thumb_image.nil? %>
  <p class="card-text">
    <span><%= link_to portfolio_item.title, portfolio_show_path(portfolio_item) %></span> <%= portfolio_item.subtitle %>
  </p>
</div>

我需要提供任何其他帮助的信息吗? 谢谢!

1 个答案:

答案 0 :(得分:0)

您是否检查过carrierwave gem是否已正确安装?检查以下命令:

bundle show carrierwave

如果已安装但仍无法正常工作,则可能必须在自动加载路径中添加/app/uploaders,以使“上传者”可以看到。

选中此issue