rails custom validation on action -


i know if there's way use rails validations on custom action.

for example this:

validates_presence_of :description, :on => :publish, :message => "can't blank" 

i basic validations create , save, there great many things don't want require front. ie, should able save barebones record without validating fields, have custom "publish" action , state in controller , model when used should validate make sure record 100%

the above example didn't work, ideas?

update:

my state machine looks this:

  include activerecord::transitions   state_machine      state :draft     state :active     state :offline      event :publish       transitions :to => :active, :from => :draft, :on_transition => :do_submit_to_user, :guard => :validates_a_lot?     end    end 

i found can add guards, still i'd able use rails validations instead of doing on custom method.

that looks more business logic rather model validation me. in project few years ago in had publish articles, , lots of business rules enforced @ moment.

i suggest model.publish() , method should enforce business rules in order item published.

one option run custom validation method, might need add fields model. here's example - i'll assume model called article

class article < activerecord::base   validate :ready_to_publish    def publish     self.published = true     //and else need in order mark article published   end    private   def ready_to_publish     if( published? )        //checks fields set        errors.add(:description, "enter description") if self.description.blank?     end   end end 

in example, client code should call an_article.publish , when article.save invoked rest automatically. other big benefit of approach model consistent, rather depending on action invoked.


Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -