Rails 3 - Form Validation - Pretest?
I'm having some issues using rails validation on a form. What I'm doing is, in a form users are entering a URL for some of their favorite sites. When they submit, my create method uses nokogiri to open to URL and parse the header of the web page. The problem I'm running into is, if the user enters the form and it's blank, my create method tried to open nil, whi开发者_C百科ch causes is to error out. I tried to add validation to the method like so:
validates :url, :presence => true
If the user enters the form, this validation does not catch the empty text area, and fails as it tried to open the path before checking the validation. Is there a way in rails to specify I want to run the validation at the start of the create rather the end (which I'm assuming is happening)?
Why not call nokogiri in an after_validation
or after_create
callback? This would elegantly solve your problem.
精彩评论