Paperclip: Stay edit

When a user edits something in their application, they are forced to reload their image through a paper clip, even if they do not change it. Otherwise, this will lead to an error, since I validate_presence_of: image. This is pretty annoying.

How can I do this, so Paperclip will not update its attributes unless the user simply adds a new image to the edit?

The Photos Controller is fresh from the Rails Flyover Generator. The rest of the source code is below.

models /accommodation.rb

class Accommodation < 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 => true end 

Controllers / accommodation _controller.rb

 class AccommodationsController < 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 

models /photo.rb

 class Photo < ActiveRecord::Base attr_accessible :image, :primary belongs_to :accommodation has_attached_file :image, :styles => { :thumb=> "100x100#", :small => "150x150>" } end 
+4
source share
1 answer

You do not need @accommodation.build_photo anywhere except the new action.

+1
source

Source: https://habr.com/ru/post/1334301/


All Articles