问题
I want to validate column presence on creation method if the creation method is called from some other route. For example, If I have following two routes:
post 'create_item', to: 'item#create'
post 'create_verified_item', to: 'item#create_verified'
I need to define in Item model something like this:
validates :verified_number, presence: true, if: Item.action_name == "create_verified"
Anyone can help?
回答1:
Ideally you can add a attribute to item to check that, something like:
# model
class Item
attr_accessible :action_name
validates :verified_number, presence: true, if: :create_verified?
def create_verified?
action_name == 'create_verified'
end
end
# controller
item = Item.new(item_params)
item.action_name = params[:action]
item.save
来源:https://stackoverflow.com/questions/46211747/how-to-check-validation-on-creation-method-if-its-called-from-specific-route