Eric R.
Eric R.

Reputation: 983

Paperclip: "missing" image

I'm working on a website that allows people who run bed and breakfast businesses to post their accommodations.

I would like to require that they include a "profile image" of the accommodation when they post it, but I also want to give them the option to add more images later (this will be developed after).

I thought the best thing to do would be to use the Paperclip gem and have a Accommodation and a Photo in my application, the later belonging to the first as an association.

A new Photo record is created when they create an Accommodation. It has both id and accommodation_id attributes. However, the image is never uploaded and none of the Paperclip attributes get set (image_file_name: nil, image_content_type: nil, image_file_size: nil), so I get Paperclip's "missing" photo.

Any ideas on this one? It's been keeping me stuck for a few days now.

Accommodation

models/accommodation.rb

class Accommodation < ActiveRecord::Base
  validates_presence_of :title, :description, :photo, :thing, :location
  attr_accessible :title, :description, :thing, :borough, :location, :spaces, :price
  has_one :photo
end

controllers/accommodation_controller.erb

class AccommodationsController < ApplicationController

  before_filter :login_required, :only => {:new, :edit}

  uses_tiny_mce ( :options => {
    :theme => 'advanced',
    :theme_advanced_toolbar_location => 'top',
    :theme_advanced_toolbar_align => 'left',
    :theme_advanced_buttons1 => 'bold,italic,underline,bullist,numlist,separator,undo,redo',
    :theme_advanced_buttons2 => '',
    :theme_advanced_buttons3 => ''
  })

  def index
    @accommodations = Accommodation.all
  end

  def show
    @accommodation = Accommodation.find(params[:id])
  end

  def new
    @accommodation = Accommodation.new
  end

  def create
    @accommodation = Accommodation.new(params[:accommodation])
    @accommodation.photo = Photo.new(params[:photo])
    @accommodation.user_id = current_user.id
    if @accommodation.save
      flash[:notice] = "Successfully created your accommodation."
      render :action => 'show'
    else
      render :action => 'new'
    end
  end

  def edit
    @accommodation = Accommodation.find(params[:id])
  end

  def update
    @accommodation = Accommodation.find(params[:id])
    if @accommodation.update_attributes(params[:accommodation])
      flash[:notice] = "Successfully updated accommodation."
      render :action => 'show'
    else
      render :action => 'edit'
    end
  end

  def destroy
    @accommodation = Accommodation.find(params[:id])
    @accommodation.destroy
    flash[:notice] = "Successfully destroyed accommodation."
    redirect_to :inkeep
  end
end

views/accommodations/_form.html.erb

<%= form_for @accommodation, :html => {:multipart => true} do |f| %>
  <%= f.error_messages %>
  <p>
    Title<br />
    <%= f.text_field :title, :size => 60 %>
  </p>
  <p>
    Description<br />
    <%= f.text_area :description, :rows => 17, :cols => 75, :class => "mceEditor" %>
  </p>
  <p>
    Photo<br />
    <%= f.file_field :photo %>
  </p>
  [... snip ...]
 <p><%= f.submit %></p>
<% end %>

Photo

The controller and views are still the same as when Rails generated them.

models/photo.erb

class Photo < ActiveRecord::Base
  attr_accessible :image_file_name, :image_content_type, :image_file_size
  belongs_to :accommodation
  has_attached_file :image,
    :styles => {
      :thumb=> "100x100#",
      :small  => "150x150>" }
end

Upvotes: 3

Views: 5038

Answers (1)

iain
iain

Reputation: 16274

To create an upload with paperclip, you need to use the name you provided for the has_attached_file line, on the model you defined it on. In your case, this will result in this view code:

<%= form_for @accommodation, :html => { :multipart => true } do |f| %>
  <%= f.fields_for :photo do |photo_fields| %>
    <p>
      Photo<br />
      <%= photo_fields.file_field :image %>
    </p>
  <% end %>
<% end %>

In the controller:

class AccommodationsController < ApplicationController

  # also protect create and update actions!
  before_filter :login_required, :only => [ :new, :create, :edit, :update ]

  def new
    # always make objects through their owner
    @accommodation = current_user.accommodations.build
    @accommodation.build_photo
  end

  def create
    @accommodation = current_user.accommodations.build(params[:accommodation])
    if @accommodation.save
       # always redirect after successful save/update
       redirect_to @accommodation
    else
       render :new
    end
  end
end

Tell Rails to handle the nested form:

class Accommodation
  has_one :photo
  accepts_nested_attributes :photo
  attr_accessible :photo_attributes, :title, :description, :etc
end

And make sure to set the accessible attributes right in your photo model:

class Photo
  attr_accessible :image # individual attributes such as image_file_name shouldn't be accessible
  has_attached_file :image, :styles => "etc"
end

Be sure to watch your log files to spot things that are protected by attr_accessible, but still are in your form.

Upvotes: 2

Related Questions