Note that there are some explanatory texts on larger screens.

plurals
  1. POPaperclip: "missing" image
    text
    copied!<p>I'm working on a website that allows people who run bed and breakfast businesses to post their accommodations.</p> <p>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).</p> <p>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.</p> <p>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.</p> <p>Any ideas on this one? It's been keeping me stuck for a few days now.</p> <h2>Accommodation</h2> <p><strong>models/accommodation.rb</strong></p> <pre><code>class Accommodation &lt; ActiveRecord::Base validates_presence_of :title, :description, :photo, :thing, :location attr_accessible :title, :description, :thing, :borough, :location, :spaces, :price has_one :photo end </code></pre> <p><strong>controllers/accommodation_controller.erb</strong></p> <pre><code>class AccommodationsController &lt; ApplicationController before_filter :login_required, :only =&gt; {:new, :edit} uses_tiny_mce ( :options =&gt; { :theme =&gt; 'advanced', :theme_advanced_toolbar_location =&gt; 'top', :theme_advanced_toolbar_align =&gt; 'left', :theme_advanced_buttons1 =&gt; 'bold,italic,underline,bullist,numlist,separator,undo,redo', :theme_advanced_buttons2 =&gt; '', :theme_advanced_buttons3 =&gt; '' }) 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 =&gt; 'show' else render :action =&gt; '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 =&gt; 'show' else render :action =&gt; 'edit' end end def destroy @accommodation = Accommodation.find(params[:id]) @accommodation.destroy flash[:notice] = "Successfully destroyed accommodation." redirect_to :inkeep end end </code></pre> <p><strong>views/accommodations/_form.html.erb</strong></p> <pre><code>&lt;%= form_for @accommodation, :html =&gt; {:multipart =&gt; true} do |f| %&gt; &lt;%= f.error_messages %&gt; &lt;p&gt; Title&lt;br /&gt; &lt;%= f.text_field :title, :size =&gt; 60 %&gt; &lt;/p&gt; &lt;p&gt; Description&lt;br /&gt; &lt;%= f.text_area :description, :rows =&gt; 17, :cols =&gt; 75, :class =&gt; "mceEditor" %&gt; &lt;/p&gt; &lt;p&gt; Photo&lt;br /&gt; &lt;%= f.file_field :photo %&gt; &lt;/p&gt; [... snip ...] &lt;p&gt;&lt;%= f.submit %&gt;&lt;/p&gt; &lt;% end %&gt; </code></pre> <h2>Photo</h2> <p>The controller and views are still the same as when Rails generated them.</p> <p><strong>models/photo.erb</strong></p> <pre><code>class Photo &lt; ActiveRecord::Base attr_accessible :image_file_name, :image_content_type, :image_file_size belongs_to :accommodation has_attached_file :image, :styles =&gt; { :thumb=&gt; "100x100#", :small =&gt; "150x150&gt;" } end </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload