Hi all, I have a controller named aircrafts. In there I put: def showtime @time = Time.now end In the default views/layouts/aircrafts.rhtml, I want to use the method above to show the current time on the page. But adding <%= @time %> does nothing. Do I have to have some kind of <%= showtime.time %> syntax or something, like in Java? Thanks. New to Ruby, and trying to learn as I go, so please forgive some of these ''simple'' questions. Hussein. -- 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 -~----------~----~----~----~------~----~------~--~---
Hussein Patwa wrote:> Hi all, > > I have a controller named aircrafts. In there I put: > > def showtime > @time = Time.now > end > > In the default views/layouts/aircrafts.rhtml, I want to use the method > above to show the current time on the page. > > But adding <%= @time %> does nothing. Do I have to have some kind of > <%= showtime.time %> syntax or something, like in Java? > > Thanks. New to Ruby, and trying to learn as I go, so please forgive > some of these ''simple'' questions. > > Hussein.Hiya Hussein, It doesnt quite work like that. I''m sure someone will be able to explain why better than me. To do what you want though you can use helpers. Look in the directory /app/helpers/aircrafts_helper.rb Add the code above into there and you will be able to access it as you wish Cheers Luke -- 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 -~----------~----~----~----~------~----~------~--~---
Opps forgot to add if you put that into application_helper.rb then it will be available to all of your views. -- 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 -~----------~----~----~----~------~----~------~--~---
Hussein Patwa wrote:> Hi all, > > I have a controller named aircrafts. In there I put: > > def showtime > @time = Time.now > end > > In the default views/layouts/aircrafts.rhtml, I want to use the method > above to show the current time on the page. > > But adding <%= @time %> does nothing. Do I have to have some kind of > <%= showtime.time %> syntax or something, like in Java? > > Thanks. New to Ruby, and trying to learn as I go, so please forgive > some of these ''simple'' questions. > > Hussein.Methods in the controller generally map to the view of that name. So, if you have a file called views/showtime.rhtml with <%= @time %>, it will display correctly. You need to change it to <%= showtime %> if you want it to work. However, this method should not be in the controller. It should probably be in helpers/aircraft_helper.rb or helpers/application_helper.rb I''m still green to rails, so there could be a better way, but both of my options will work. -- 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 -~----------~----~----~----~------~----~------~--~---
Hi. Ok, so I guess the helpers files act somewhat like your java superclasses. Would the functions be accessed just through their name as in @time? Also, how would you access controller-speific functions, as in the one I had in my original post? Thanks. Hussein. -- 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 -~----------~----~----~----~------~----~------~--~---
Hussein Patwa wrote:> > Also, how would you access controller-speific functions, as in the one I > had in my original post? >You can create a function in the controller and use the helper_method specifier. eg class ApplicationController < ActionController::Base helper_method :give_time def give_time Time.now # return the current time end end Then in you view, you can put a <%= give_time %> In fact, helper_method makes a method visible both in the controller and in the view. But, IMHO you should take care of not making wrong usage of this specifier. In most cases you just have to put these methods in the application_helper.rb which makes it visible in all views. -- 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 -~----------~----~----~----~------~----~------~--~---
Hussein Patwa wrote:> Hi all, > > I have a controller named aircrafts. In there I put: > > def showtime > @time = Time.now > endHi, In Ruby 1)if you declare a method without associating it to any class you just call it directly: showtime 2) if you declare a method within a class say X without using private, protected, or public you call it publicly(default way in Ruby) obj=X.new obj.showtime 3) if you run them from within the script you need to add "puts" to see the results: puts showtime puts obj.showtime Li ### C:\>irb irb(main):001:0> def showtime irb(main):002:1> @time = Time.now irb(main):003:1> end => nil irb(main):004:0> showtime => Thu Dec 14 13:42:19 -0500 2006 irb(main):005:0> class X irb(main):006:1> def showtime irb(main):007:2> @time=Time.now irb(main):008:2> end irb(main):009:1> end => nil irb(main):010:0> obj=X.new => #<X:0x2c22b74> irb(main):011:0> obj.showtime => Thu Dec 14 13:43:12 -0500 2006 irb(main):012:0> -- 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 -~----------~----~----~----~------~----~------~--~---
Hi and thanks for the input. I think I understand now. I''ll work on this tonight and post back if there''s anything else. Hussein. -- 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 -~----------~----~----~----~------~----~------~--~---
Honestly I would just put the code in the view: <%= Time.now %> If you use the time in many views and want to standardize the formatting, go with application_helper.rb -- 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 -~----------~----~----~----~------~----~------~--~---