0

I'm building a simple app with standard User model that has_one Profile model. I would like the profiles table to have a column for an image attribute. Therefore, I followed RailsCast #253 titled CarrierWave File Uploads. All is going well until I try to resize an image that has been uploaded. This requires the installation of ImageMagick & RMagick which took an entire day of searching to get done. However, I think I finally got it right and successfully installed rmagick version 2.13.2 (as verified by running "gem list").

But not so fast...now when I try to render the form to create a new profile, I get the following error:

NoMethodError in Profiles#new undefined method `model_name' for NilClass:Class

FYI, this form was working fine until I un-commented "include CarrierWave::RMagick" in my ImageUploader (which I'm suppose to do if I want to use RMagick methods for image re-sizing).

Any thoughts on how to fix this?

My version info (I used RailsInstaller for Windows to get up & running)

Rails 3.2.13
Ruby 1.9.3p392 [i386-minw32]
ImageMagick 6.8.5-Q16
rmagick 2.13.2

Gemfile

gem 'rmagick'
gem 'carrierwave'

Models

class User < ActiveRecord::Base 
  has_one :profile

class Profile < ActiveRecord::Base
  attr_accessbile :image
  belongs_to :user
  mount_uploader :image, ImageUploader

ImageUploader

class ImageUploader < CarrierWave::Uploader::Base

  include CarrierWave::RMagick

  storage :file

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

ProfilesController

def new
  @profile = current_user.build_profile
end

views/profiles/new.html.erb

<%= form_for @profile, :html => {:multipart => true} do |f| %>

  <%= f.file_field :image %>

  <%= f.submit "Create my profile" %>

<% end %>
mattq
  • 89
  • 10

1 Answers1

0

Just replace this line

<%= form_for @profile, :html => {:multipart => true} do |f| %>

by

<%= form_for @instructor_profile, :html => {:multipart => true} do |f| %>

Issue is really with form_for not with RMagick, you have defined @instructor_profile in controller but using @profile in view. Make sure that there will be single space between form_for and @instructor_profile

maximus ツ
  • 7,949
  • 3
  • 25
  • 54
  • Hi Maximus. Sorry, I was trying to omit the instructor_ to keep my question a little more brief. I have edited it now. – mattq May 07 '13 at 16:42
  • There looks like a typo, are you sure there is space between form_for and @profile? – maximus ツ May 07 '13 at 16:45
  • Yes. I'm certain. Also, it works properly as soon as I comment out "include CarrierWave::RMagick" in the ImageUploader. However, this would not be an effective fix b/c I would like to be able to use RMagick for image re-sizing. – mattq May 07 '13 at 16:49
  • Strange, can you please remove image field from form and check again? Also check @profile is not nil. – maximus ツ May 07 '13 at 17:16
  • (1) With image field removed I still get the same error. (2) How can I check if @profile is nil? – mattq May 07 '13 at 17:22
  • I have verified that @profile is not nil. The problem definitely seems to be coming from mounting the ImageUploader (when include CarrierWave::RMagick is activated). – mattq May 07 '13 at 17:33