現在、サイトへの写真のアップロードに CarrierWave を使用しています。問題は、写真/アップロード (upload.html.erb) を開くと、次のエラーが表示されることです。
NilClass:Class の未定義メソッド `model_name'
私の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 %>
私のモデル(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
よろしく、 イヴァン