現在我使用CarrierWave將照片上傳到我的網站。問題是,當我打開照片/上傳(upload.html.erb)告訴我這個錯誤:「載體類型爲NilClass:Class」的undefined方法`model_name「
undefined method `model_name' for NilClass:Class
圍繞我upload.html.erb線1:
<%= form_for @photos, :html => {:multipart => true} do |f| %>
<%= f.error_messages %>
<%= f.hidden_field :gallery_id %>
<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
<p>
<%= f.file_field :photo %>
</p>
<p><%= f.submit %></p>
<% end %>
我不「T真正理解這一點,因爲我覺得我的模型(photos.rb)還沒有得到錯誤:
class Photos < ActiveRecord::Base
attr_accessor :gallery_id, :name, :photo
belongs_to :gallery
mount_uploader :photos, PhotosUploader
end
在我photos_controller.rb我有這樣的:
class PhotosController < ApplicationController
def new
@photos = Photos.new(:gallery_id => params[:gallery_id])
end
def create
@photos = Photos.new(params[:photos])
if @photos.save
flash[:notice] = "Successfully created Photos."
else
render :action => 'new'
end
end
def edit
@photos = Photos.find(params[:id])
end
def update
@photos = Photos.find(params[:id])
if @photos.update_attributes(params[:photos])
flash[:notice] = "Successfully updated Photos."
else
render :action => 'edit'
end
end
def destroy
@photos = Photos.find(params[:id])
@photos.destroy
flash[:notice] = "Successfully destroyed Photos."
end
end
這我photos_uploader.rb:
class PhotosUploader < CarrierWave::Uploader::Base
include CarrierWave::RMagick
storage :file
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
version :thumb do
process :scale => [180, 180]
end
end
的問候,伊萬
您的遷移等排序好嗎? –
是的,我已經完成了我的遷移。 – Ivanhercaz