4

Now I'm using CarrierWave for upload photos to my site. The problem is that when I open photos/upload (upload.html.erb) show me this error:

undefined method `model_name' for NilClass:Class

Around the line 1 of my upload.html.erb:

<%= 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 %>  

I don't really understand this, because I think that my model (photos.rb) hasn't got errors:

class Photos < ActiveRecord::Base
  attr_accessor :gallery_id, :name, :photo
  belongs_to :gallery
  mount_uploader :photos, PhotosUploader
end

In my photos_controller.rb I have this:

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

And this my 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

Regards, Iván

Ivanhercaz
  • 731
  • 1
  • 11
  • 31

4 Answers4

3

The problem could have something to do with the fact that you've mounted the uploader as :photos, but the form field is :photo. Try making these match.

Luke
  • 4,855
  • 1
  • 22
  • 18
  • Yes, a small error, sorry. But, when I change :photo to :photos, the error continue. I will keep looking for the error. – Ivanhercaz Jun 24 '11 at 14:34
  • 1
    Also, your model is named `Photos` and should really be named `Photo`. Models names are singular by convention in Rails and this could be the reason it can't find the correct model. It also makes everything a little more confusing when I'm trying to look for inconsistencies. Try changing this and updating your question to match. – Luke Jun 24 '11 at 18:57
  • 1
    Good eye Luke. As for the fields, he's got them defined correctly `attr_accessor :gallery_id, :name, :photo`. Yes, the model needs to be singular. – Michael De Silva Jun 26 '11 at 06:36
  • 1
    This may not solve the problem, but it will give us a better idea on what the error might be. – Alex Siri Dec 29 '13 at 18:34
1

I am answering it late as in can see there is no upload action in photos controller that matches with template upload.html.erb

I suggest you to rename it to new.html.erb and try accessing the url 'photos/new'

Sachin Singh
  • 7,107
  • 6
  • 40
  • 80
1

The Problem can be fixed by instantiating the object photo object. I think that should fix the problem.

Gaurav
  • 81
  • 1
  • 6
0

I had the same problem and searching got me to this answer https://stackoverflow.com/a/21992187/3280686

You need to add this on your Class PhotosUploader

include CarrierWaveDirect::Uploader
include ActiveModel::Conversion
extend ActiveModel::Naming
roxdurazo
  • 745
  • 10
  • 21