The Guide says about /lib directory:
lib/ Extended modules for your application.
Somewhere I read that I could place there my custom email validator
class. But how to use it then? How to require it?
Now I do this way in my "create" method:
def create
require "myEmailValidator"
@result = EmailValidator.validate(params[:email_from_form])
end
It works, but maybe is there a better way to work with it?
--
Posted via http://www.ruby-forum.com/.
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit https://groups.google.com/groups/opt_out.
Wins Lin wrote in post #1105843:> # In my "create" methon > def create > require "myEmailValidator" > @result = EmailValidator.validate(params[:email_from_form]) > end > > It works, but maybe is there a better way to work with it?Isn''t it generally accepted practice to put require statements at the top of the file? It seem odd to me to put them inside a method as you''ve done here. Maybe I''m just used to treating require like import from other languages. You may find this helpful: http://reefpoints.dockyard.com/ruby/2012/02/14/love-your-lib-directory.html -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
what i usually do is adding autoload_path.
in config/application.rb, i append
config.autoload_paths += %W(#{Rails.root}/lib/validators)
then i put generic validators such as EmailValidator into lib/validators
directory
after that, all i need is
class SomeModel < ActiveRecord::Base
validates :email, :email => true
end
2013/4/16 Wins Lin <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org>
> The Guide says about /lib directory:
> lib/ Extended modules for your application.
>
> Somewhere I read that I could place there my custom email validator
> class. But how to use it then? How to require it?
>
> Now I do this way in my "create" method:
>
> def create
> require "myEmailValidator"
> @result = EmailValidator.validate(params[:email_from_form])
> end
>
> It works, but maybe is there a better way to work with it?
>
> --
> Posted via http://www.ruby-forum.com/.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
> To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
>
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit https://groups.google.com/groups/opt_out.