validation - Rails validate only in admin namespace -



validation - Rails validate only in admin namespace -

in rails app have 2 user-logic:

user, , admin

for illustration routing line_item such:

namespace :admin resources :line_items, :only => [:edit, :update, :destroy] end resources :line_items, :only => [:new, :create, :update, :edit, :destroy]

but in admin part must vailation line_item model:

validates :notes, :presence => {:message => i18n.t(:notes_not_chosen)} validates :quantity, :presence => {:message => i18n.t(:quantity_not_chosen)} validates :price, :presence => {:message => i18n.t(:price_not_chosen)} validates :description, :presence => {:message => i18n.t(:description_not_chosen)}

but in admin controller! how have validations in admin line_item controller, didn't have in user part?

i don't think can validations based on controller request came through. can add together user attribute model , set in controller. can base of operations validation on user attribute. so:

controller:

def create @line_item = lineitem.new(params[:line_item]) @line_item.user = current_user if @line_item.save # ... standard rails stuff end

model:

class lineitem < activerecord::base attr_accessor :user validates :notes, :presence => true, :if => proc.new { user.admin? } end

ruby-on-rails validation model

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -