# Undefined method \`content\_type' when editing already-uploaded items (attachment\_fu)

**URL:** <https://www.ruby-forum.com/t/undefined-method-content-type-when-editing-already-uploaded-items-attachment-fu/160745>\
**Category:** Rails\
**Created:** [February 19, 2009, 8:44am UTC](https://www.ruby-forum.com/t/undefined-method-content-type-when-editing-already-uploaded-items-attachment-fu/160745 "2009-02-19T08:44:59Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![adammontgomery](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ce7236/32.png) [@adammontgomery](https://www.ruby-forum.com/u/adammontgomery)\
**Post date:** [February 19, 2009, 8:44am UTC](https://www.ruby-forum.com/t/undefined-method-content-type-when-editing-already-uploaded-items-attachment-fu/160745/1 "2009-02-19T08:44:59Z")

</div>

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. ☹

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.

---

<div class="post-metadata">

**Author:** ![adammontgomery](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ce7236/32.png) [@adammontgomery](https://www.ruby-forum.com/u/adammontgomery)\
**Post date:** [February 19, 2009, 7:10pm UTC](https://www.ruby-forum.com/t/undefined-method-content-type-when-editing-already-uploaded-items-attachment-fu/160745/2 "2009-02-19T19:10:06Z")

</div>

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.
