Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4606026
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T00:30:37+00:00 2026-05-22T00:30:37+00:00

I have a preview of attached image in my update form. The problem appears

  • 0

I have a preview of attached image in my update form. The problem appears when user gets validation errors on attachment field. In this case image thumbnail url becomes as if an image was uploaded without any errors (it shows name of file that was not saved at server).

Here is how I get image url in my view:
<%= image_tag(@product.photo.url(:medium)) %>.

Controller:

def update
  @product = Product.find(params[:id])
  @product.update_attributes(params[:product]) ? redirect_to('/admin') : render(:new)
end

def edit
  @product = Product.find(params[:id])
  render :new
end

Model:

class Product < ActiveRecord::Base

  <...>

  @@image_sizes = {:big => '500x500>', :medium => '200x200>', :thumb=> '100x100>'}

  has_attached_file :photo, :styles => @@image_sizes, :whiny => false
  validates_attachment_presence :photo
  validates_attachment_content_type :photo, :content_type => ['image/jpeg', 'image/jpg', 'image/png', 'image/gif'], :message => I18n.t(:invalid_image_type)
  validates_attachment_size :photo, :less_than => 1.megabytes, :message => I18n.t(:invalid_image_size, :max => '1 Mb')
  after_post_process :save_image_dimensions

  <...>

end

UPD: The simpliest solution is to add @photo_file_url = @product.photo.url(:medium) in controller’s update action before @product.update_attributes and <% @photo_file_url ||= @product.photo.url(:medium) %> in the view.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T00:30:37+00:00Added an answer on May 22, 2026 at 12:30 am

    I actually had the same issue and here is what I had done (which seems a bit of a hack) but works and will show the default or previous image on update failure

    after_validation :logo_reverted?
    
    def logo_reverted?
      unless self.errors[:logo_file_size].blank? or self.errors[:logo_content_type].blank?
        self.logo.instance_write(:file_name, self.logo_file_name_was) 
        self.logo.instance_write(:file_size, self.logo_file_size_was) 
        self.logo.instance_write(:content_type, self.logo_content_type_was)
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a field with enums: 'preview','active','closed' When I query like this: $query =
I have a problem with an image preview that comes up when you hover
I want to create a preview image before the actual form upload. I have
I have this script $('table#preview td.price').each(function(){ var price = parseInt($(this).html()); var total; if(price ==
I have some columns with image data type and I want to preview (or
I have tried this but it doesn't work: tell application Preview set myfile to
I have: form.php preview.php form.php has a form in it with many dynamically created
Why does this code not work? preview[currentPreview].frame = CGRectMake(0, 320, 480, 320); preview[currentPreview].image =
I have a live preview form: http://davidwalsh.name/jquery-comment-preview Which uses jquery to display the preview
I have flatpage attached to multiple sites. Its admin preview chooses arbitrary site, which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.