我在实施carrierwave时出现以下错误,以便上传图片
gem'carrierwave','〜> 1.0' Rails 5.0.1 ruby 2.3.3p222(2016-11-21修订版56859)[i386-mingw32]
我的模特:
class Company < ApplicationRecord
mount_uploader :cover, CompanyAvatarUploader
attr_accessor :cover
My CompanyAvatarUploader
class CompanyAvatarUploader < 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
我的company_controller.erb
class CompanyController < ApplicationController
def create
@company = Company.new(company_params)
respond_to do |format|
if @company.save
format.html { redirect_to @company, notice: 'Company saved...'}
format.json { render :show, status: :created, location: @company}
else
format.html { render new}
format.json { render json: @company.errors, status: :unprocessable_entity }
end
private
def company_params
params.require(:company).permit(:name, :address, :country, :telephone, :cover)
end
end
我的new.html.erb
<p>
<%= f.label :country %><br>
<%= f.text_area :country %>
</p>
<p>
<%= f.label :cover %><br>
<%= f.file_field :cover %>
</p>
我的show.html.erb
<p>
<strong>Country:</strong>
<%= @company.country %>
</p>
<p>
<%= image_tag @company.cover.url %>
</p>
当我尝试添加带有封面的新公司时,我遇到以下错误:
NoMethodError in Company#show
Showing
app/views/company/show.html.erb where line #22 raised:
undefined method `url' for nil:NilClass
Extracted source (around line #22):
</p>
<p>
<%= image_tag @company.cover.url %>
</p>
公司信息存储在我的MySQL中但不是封面
感谢您的帮助