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 7970973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:31:47+00:00 2026-06-04T07:31:47+00:00

I have a model named Profile which: has_attached_file :avatar I’m validating it to only

  • 0

I have a model named Profile which:

has_attached_file :avatar

I’m validating it to only accept images, with:

  validates_attachment_size :avatar,:less_than => 2.megabytes
  validates_attachment_content_type :avatar, :content_type => /^image\/(jpg|jpeg|pjpeg|png|x-png|gif)$/, :message => 'file type is not allowed (only jpeg/png/gif images)'

And the controller action is implemented like this:

@profile = Profile.find(params[:id])
if @profile.update_attributes(params[:profile])
  redirect_to @profile, notice: "Profile was updated"
else
  from_render = params[:render]
  from_render = "show" if !["show","edit"].include?(from_render)
  respond_to do |format|
    format.html { render :action => from_render }
  end
end

And in the view, I’m displaying it like:

  <%= image_tag profile.avatar.url(:medium) %>

I’m allowing the picture to be changed from both show and edit actions, hence the else ( to know where to redirect in case of an error ). The problem is, if I upload a textfile, the view will try to render an image tag, that has a href to the text file. This results in something incorrect being rendered. So, now, profile.avatar points to the file, and the attachment isn’t saved. How can I fallback on the original image?

  • 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-06-04T07:31:49+00:00Added an answer on June 4, 2026 at 7:31 am

    I kept a copy of the old object, before update, and if update failed, I showed that.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following model: class UserProfile(models.Model): User profile model, cintains a Foreign Key, which
I have a user model which has_one profile. The user model has a name
I have the following models: class Profile(models.Model): verified = models.BooleanField(default=False) primary_phone = models.OneToOneField('Phone', related_name='is_primary',
I have a model named Aggelia (it's greek for classified ad) and have set
I have a such model named Foo: class Foo(models.Model): name = models.CharField() entry =
I have one grails application.In that I have one model class named Book. From
I have a controller/model hypothetically named Pets. Pets has the following declarations: belongs_to :owner
I have a books model with a date type column named publish_date. On my
In my app, I have a User model, which includes a number of attributes
I have a profile class: class profile(db.Model): user = db.stringProperty() # Other properties ...

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.