Undefined method `content_type' when editing already-uploaded items (attachment_fu)


#1

My project is for a photographer’s portfolio. He’s allowed to upload
photographs, which are then displayed on the site. I’m using
attachment_fu. All of this works fine.

Where I’m running into problems is in allowing him to edit the
photographs’ attributes later.

Example:
I’ve uploaded a picture of a spider. During the upload I enter the
title as “spider 1”. Great, the photo uploads, and if I go to to
admin/index, there it is. Glorious. But oops! I forgot to
capitalize my title! So I head to admin/edit/:photo_id, a form
displays, and I correct my mistake. Then I click Update. And oh
no. :frowning:

NoMethodError in AdminController#update

undefined method `content_type’ for #Photo:0xb67ffbf0

I’ve been working on this problem for a while now. Commenting out
everything attachment_fu related in my Photos model is the only way I
can make the edit form work (which of course breaks uploading).

Here is all relevant code:

models/photo.rb:

class Photo < ActiveRecord::Base

attr_accessible :title, :description, :camera, :time, :date, :iso,
:lens,
:focal_length, :exposure, :aperture

  has_attachment :content_type => :image,
                 :storage => :file_system,
                 :path_prefix => 'public/photos',
                 :thumbnails => { :thumb => '320x200>' }
  validates_as_attachment
  validates_uniqueness_of :filename

  def full_filename(thumbnail = nil)
    file_system_path = (thumbnail ? thumbnail_class :

self).attachment_options[:path_prefix].to_s
File.join(RAILS_ROOT, file_system_path, thumbnail_name_for
(thumbnail))
end

  def exifize(photo)
    require 'mini_exiftool'
    @photo = photo
    @exif = MiniExiftool.new "public/photos/#{ @photo.filename }"
    @photo.camera = @exif.model
    @photo.time = @exif.datetimeoriginal.strftime('%I:%M %p')
    @photo.date = @exif.datetimeoriginal.strftime('%B %d, %Y')
    @photo.iso = @exif.iso
    @photo.lens = @exif.lens
    @photo.focal_length = @exif.focal_length
    @photo.aperture = @exif.aperture
    @photo.save
  end
end

controllers/admin_controller.rb:

def new
  @photo = Photo.new(params[:photo])
end

def create
  @photo = Photo.new(params[:photo])
  @photo.exifize(:photo)
  @photo.save
  if @photo.save
    flash[:notice] = 'Yay!'
    redirect_to :controller => 'admin', :action => :index
  else
    flash[:error] = 'boo...'
    redirect_to :controller => 'admin', :action => :error
  end
end

views/admin/edit.html.erb:

<% form_for :photo, :url => { :controller => 'admin',
                              :action => 'update',
                              :id => @photo } do |f| %>

  <p>
    <%= f.label :title %><br />
    <%= f.text_field :title %>
  </p>
  <p>
    <%= f.label :description %><br />
    <%= f.text_area :description %>
  </p>
  <p>
    <%= f.label :camera %><br />
    <%= f.text_field :camera %>
  </p>

  ...etc...

  <p>
    <%= f.submit "Update" %>
  </p>
<% end %>

I’m lost. Please help.


#2

Reading documentation thoroughly is always good. A member of
#rubyonrails on freenode helped me out. Since uploading worked fine,
I didn’t think to check my model. I had neglected to add the
content_type column. Oops.