LondonGuy
LondonGuy

Reputation: 11098

How do I bypass validation rules set for a nested attribute in ruby on rails?

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

Upvotes: 0

Views: 2237

Answers (1)

Adam
Adam

Reputation: 3158

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.

Upvotes: 2

Related Questions