Note that there are some explanatory texts on larger screens.

plurals
  1. POPaperclip: Stay put on edit
    primarykey
    data
    text
    <p>When a user edits something in my application, they're forced to re-upload their image via paperclip even if they aren't changing it. Failing to do so will cause an error, since I validate_presence_of :image. This is quite annoying.</p> <p>How can I make it so Paperclip won't update its attributes if a user simply doesn't supply a new image on an edit?</p> <p>The photo controller is fresh out of Rails' scaffold generator. The rest of the source code is provided below.</p> <p><strong>models/accommodation.rb</strong></p> <pre><code>class Accommodation &lt; ActiveRecord::Base attr_accessible :photo validates_presence_of :photo has_one :photo has_many :notifications belongs_to :user accepts_nested_attributes_for :photo, :allow_destroy =&gt; true end </code></pre> <p><strong>controllers/accommodation_controller.rb</strong></p> <pre><code>class AccommodationsController &lt; ApplicationController def index @accommodations = Accommodation.all end def show @accommodation = Accommodation.find(params[:id]) rescue ActiveRecord::RecordNotFound flash[:error] = "Accommodation not found." redirect_to :home end def new @accommodation = current_user.accommodations.build @accommodation.build_photo end def create @accommodation = current_user.accommodations.build(params[:accommodation]) if @accommodation.save flash[:notice] = "Successfully created your accommodation." redirect_to @accommodation else @accommodation.build_photo render :new end end def edit @accommodation = Accommodation.find(params[:id]) @accommodation.build_photo rescue ActiveRecord::RecordNotFound flash[:error] = "Accommodation not found." redirect_to :home end def update @accommodation = Accommodation.find(params[:id]) if @accommodation.update_attributes(params[:accommodation]) flash[:notice] = "Successfully updated accommodation." redirect_to @accommodation else @accommodation.build_photo render :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>models/photo.rb</strong></p> <pre><code>class Photo &lt; ActiveRecord::Base attr_accessible :image, :primary belongs_to :accommodation has_attached_file :image, :styles =&gt; { :thumb=&gt; "100x100#", :small =&gt; "150x150&gt;" } end </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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