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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T14:30:44+00:00 2026-06-03T14:30:44+00:00

I have a micropost form which allows a user to upload a photo and

  • 0

I have a micropost form which allows a user to upload a photo and type some content to go with it. The image file field is the nested attribute from my photo model.

It has a validation rule “presence => true”. This is not required for microposts. User are allowed to post microposts without images/photos.

How ever I use the same photo model for the users image gallery and a photo is required at the time of form submission so I can’t disable this rule.

Is there any way to bypass the validation rule set in my photo model for when I post form the micropost form?

Controller:

  def new
    @user = User.new 
    @micropost = Micropost.new(:user_id => users_id)
    @micropost.build_photo(:photo_album_id => current_user.photo_albums.find_by_album_title("microposts album").id)
  end

Form:

= form_for @micropost, :html => { :multipart => true }, :remote => true do |f|
    = f.fields_for :photo do |p|
        = p.hidden_field :photo_album_id
        = p.text_field :photo_title
        = p.file_field :image, :id => "micropost_image"
    = f.hidden_field :user_id
    = f.text_area :content
        = f.submit "Post"

Micropost model:

class Micropost < ActiveRecord::Base

    belongs_to :user
    has_many :comments, :dependent => :destroy 
    has_one  :photo, :dependent => :destroy


    accepts_nested_attributes_for :photo

    attr_accessor :username 
    attr_accessible :content, :user_id, :poster_id, :username, :remote_image_url, :photo_attributes

    validates :content, :presence => true, :length => { :maximum => 10000 }
    validates :user_id, :presence => true


end

Photo model:

class Photo < ActiveRecord::Base


    belongs_to :photo_album

    attr_accessible :photo_album_id, :photo_title, :image, :remote_image_url
    mount_uploader :image, ImageUploader

    alpha_num_non_word_char = /^[a-zA-Z0-9\D_ -]*$/

    validates :image, :presence => true
    validates :photo_title, :length => { :minimum => 2, :maximum => 50 },
                              :format => { :with => alpha_num_non_word_char,
                                           :message => "error"
                                         }, :if => :photo_title?    
    validate :picture_size_validation, :if => "image?"

    def picture_size_validation
    errors[:image] << "Your photo should be less than 1MB" if image.size > 1.megabytes
    end

end

Kind regards

  • 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-03T14:30:46+00:00Added an answer on June 3, 2026 at 2:30 pm

    There’s an option, :reject_if, you can pass to accepts_nested_attributes_for so that it won’t try to create a new photo under certain conditions. It would work like this:

    accepts_nested_attributes_for :photo, :reject_if => proc { |attributes| attributes['image'].blank? }
    

    Since you specified the :id of the image field as being ‘micropost_image’, you might have to reference it within the proc like this instead:

    attributes['micropost_image']
    

    One of those two should work.

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

Sidebar

Related Questions

I have a form in my content page for which I am doing some
I have a bound form in microsoft access that allows a user to add
I have a windows form application which let an user to write in a
I have a form which includes a JQUERY datepicker attatched to an <input> on
I have a form, which sets these styles in constructor: this.SetStyle(ControlStyles.AllPaintingInWmPaint, true); this.SetStyle(ControlStyles.UserPaint, true);
(learning c#) I have a C# WPF application which when a certain form is
I have a button that has custom code which sends the form data to
I have a Microsoft Access form which will display a Microsoft Word app with
I have a working form which sends emails from the default logged in account.
I have an access database which has some sql queries loaded into it. I

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.