Hi all, I want to define some simple classes that can be created and passed around in views. I''m currently using hashes, but I want to use a class so I can assign default values. Is this possible? Where would I define these classes? Thanks! Abhik --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
apramanik wrote:> I want to define some simple classes that can be created and passed > around in views. I''m currently using hashes, but I want to use a class > so I can assign default values. Is this possible? Where would I define > these classes? Thanks!Your application''s "lib" directory will be in the Ruby load path and by default, when Rails comes across something starting with a capital letter it will "require" a file of the lower cased name (with words separated by underscores). This all means that, if you place a class MyClass in the file lib/my_class.rb then it will automatically be loaded when it is needed. This is the usual place to put classes which are not ActiveRecord models. You can then access this class from any model, view or controller code. -- 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 post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Ah ok. That worked. I had put my file in a folder under ''lib''. Is there a way to keep it in a subdirectory of ''lib'' and require it elsewhere? Abhik On Mar 22, 5:45 am, Mark Bush <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> apramanik wrote: > > I want to define some simple classes that can be created and passed > > around in views. I''m currently using hashes, but I want to use a class > > so I can assign default values. Is this possible? Where would I define > > these classes? Thanks! > > Your application''s "lib" directory will be in the Ruby load path and by > default, when Rails comes across something starting with a capital > letter it will "require" a file of the lower cased name (with words > separated by underscores). This all means that, if you place a class > MyClass in the file lib/my_class.rb then it will automatically be loaded > when it is needed. This is the usual place to put classes which are not > ActiveRecord models. You can then access this class from any model, > view or controller code. > > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
apramanik wrote:> Ah ok. That worked. I had put my file in a folder under ''lib''. Is > there a way to keep it in a subdirectory of ''lib'' and require it > elsewhere?Yes. Rails relates subdirectories to namespaces. For example, you could have the class Admin::Something in the file lib/admin/something.rb and Rails will find it automatically. If you want to put classes into subdirectories without namespaces then you need to tell Rails where to find the class definitions using "require". -- 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 post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Great that worked also. I also tried out doing a require in the erb, didn''t work. On Mar 22, 9:02 am, Mark Bush <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> apramanik wrote: > > Ah ok. That worked. I had put my file in a folder under ''lib''. Is > > there a way to keep it in a subdirectory of ''lib'' and require it > > elsewhere? > > Yes. Rails relates subdirectories to namespaces. For example, you > could have the class Admin::Something in the file lib/admin/something.rb > and Rails will find it automatically. > > If you want to put classes into subdirectories without namespaces then > you need to tell Rails where to find the class definitions using > "require". > > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
apramanik wrote:> Great that worked also. I also tried out doing a require in the erb, > didn''t work.No, you''ll have to load it in elsewhere, such as your config/environment.rb file. -- 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 post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Ok great thanks for all your help! On Mar 22, 9:27 am, Mark Bush <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> apramanik wrote: > > Great that worked also. I also tried out doing a require in the erb, > > didn''t work. > > No, you''ll have to load it in elsewhere, such as your > config/environment.rb file. > > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---