How to check validation on creation method if its called from specific route?

醉酒当歌 提交于 2019-12-12 23:42:00

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!