hei
2008-Feb-20 07:54 UTC
Strange thing with controller''s helper method and method return value
1, why my helper method does not work I hava a controller named msg, and i define a method test(id) in / app/helpers/msg_helper.rb but i can not use this method in /app/controllers/ msg_controller.rb. Can anyone tells me why ?Doedn''t rails require msg_helper.rb for me ? 2, how can i use a hash type variable returned by a method Because i can''t use helper method, i write the method just in msg_controller.rb like: :private def test(id) xx = {} xx[:test] = 1 return xx end and i find xx i get via xx = test(id) is nil I think in ruby method hash type variable will be returned by value(or maybe reference, but the variable will not out of scope) but why can i get the true value ? Thanks very much~~ My enviroment: ruby 1.8.6 (2007-09-24 patchlevel 111) [i386-mswin32] Rails 2.0.2 oracle 10 --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Visit Indonesia 2008
2008-Feb-20 08:32 UTC
Re: Strange thing with controller''s helper method and method return value
> 1, why my helper method does not work > I hava a controller named msg, and i define a method test(id) in / > app/helpers/msg_helper.rb > but i can not use this method in /app/controllers/ > msg_controller.rb. > Can anyone tells me why ?Doedn''t rails require msg_helper.rb for > me ?A helper is simply a module containing methods that assist a view not your controller. If you want make method for your controller and tobe used to many controller you can put your method in application.rb> and i find xx i get via xx = test(id) is nilSorry I not understand what do you mean on sentences above. ~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~* Reinhart Ariando YM : booking2heaven Web: http://teapoci.blogspot.com ~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~* -- 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 -~----------~----~----~----~------~----~------~--~---
hei
2008-Feb-20 12:41 UTC
Re: Strange thing with controller''s helper method and method return value
On Feb 20, 4:32 pm, Visit Indonesia 2008 <rails-mailing-l...@andreas- s.net> wrote:> > 1, why my helper method does not work > > I hava a controller named msg, and i define a method test(id) in / > > app/helpers/msg_helper.rb > > but i can not use this method in /app/controllers/ > > msg_controller.rb. > > Can anyone tells me why ?Doedn''t rails require msg_helper.rb for > > me ? > > A helper is simply a module containing methods that assist a view not > your controller. If you want make method for your controller and tobe > used to many controller you can put your method in application.rb >See a content I copy from "Agile Web Development with Rails 2nd" 18.1 Context and Dependencis Rails handles many configuration dependencies automatically; as a developer you can normally rely on it to do the right thing. For example, if a request arrives for http://my.url/store/list, Rails will do the following. 1. Load the file store_controller.rb in the directory app/ controllers. (This loading takes place only once in a production environment). 2. Instantiate an object of class StoreController. @@@ 3. Look in app/helpers for a file called store_helper.rb. If found, it is loaded and the module StoreHelper is mixed into the controller object. 4. Look in the directory app/models for a model in the file store.rb and load it if found. Do you see 3 ? is rails 2.0.2 changes this ?> > and i find xx i get via xx = test(id) is nil > > Sorry I not understand what do you mean on sentences above. >I mean , test(id) is supposed to renturn a hash with value {:text => 1} but actually , it returns nil or {} You can have a simple test Add this to one of your controller def test_action new = {} value = test_method new.merge(value) render :text => new[:text] end :private def test_method() value = {:text => 1} return value end Then visit http://root/controller/test_action to see what it returns Actually, ''value'' returned by test_method is something like flash: !map:ActionController::Flash::FlashHash :value: &id001 :text: 1 :value: *id001 I think new.merge(value) makes this Can anyone tells me more details ? Thanks!> ~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~* > Reinhart Ariando > YM : booking2heaven > Web:http://teapoci.blogspot.com > ~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~* > -- > 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Feb-20 12:51 UTC
Re: Strange thing with controller''s helper method and method return value
On 20 Feb 2008, at 12:41, hei wrote:> > > > On Feb 20, 4:32 pm, Visit Indonesia 2008 <rails-mailing-l...@andreas- > s.net> wrote: >>> 1, why my helper method does not work >>> I hava a controller named msg, and i define a method test(id) >>> in / >>> app/helpers/msg_helper.rb >>> but i can not use this method in /app/controllers/ >>> msg_controller.rb. >>> Can anyone tells me why ?Doedn''t rails require msg_helper.rb for >>> me ? >> >> A helper is simply a module containing methods that assist a view not >> your controller. If you want make method for your controller and tobe >> used to many controller you can put your method in application.rb >> > See a content I copy from "Agile Web Development with Rails 2nd" 18.1 > Context and Dependencis > Rails handles many configuration dependencies automatically; as a > developer > you can normally rely on it to do the right thing. For example, if a > request arrives for http://my.url/store/list, Rails will do the > following. > 1. Load the file store_controller.rb in the directory app/ > controllers. (This > loading takes place only once in a production environment). > 2. Instantiate an object of class StoreController. > @@@ 3. Look in app/helpers for a file called store_helper.rb. If > found, it is loaded > and the module StoreHelper is mixed into the controller object. > 4. Look in the directory app/models for a model in the file store.rb > and > load it if found.That''s just wrong - it''s never been like that.> > Do you see 3 ? is rails 2.0.2 changes this ? >>> and i find xx i get via xx = test(id) is nil >> >> Sorry I not understand what do you mean on sentences above. >> > I mean , test(id) is supposed to renturn a hash with value {:text => > 1} > but actually , it returns nil or {} > You can have a simple test > Add this to one of your controller > def test_action > new = {} > value = test_method > new.merge(value)merge doesn''t alter the hash, it returns a new hash with the merge contents (merge! does an inplace merge) Fred> > render :text => new[:text] > end > :private > def test_method() > value = {:text => 1} > return value > end > Then visit http://root/controller/test_action to see what it returns > Actually, ''value'' returned by test_method is something like > > flash: !map:ActionController::Flash::FlashHash > :value: &id001 > :text: 1 > :value: *id001 > I think new.merge(value) makes this > Can anyone tells me more details ? > Thanks! >> ~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~* >> Reinhart Ariando >> YM : booking2heaven >> Web:http://teapoci.blogspot.com >> ~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~* >> -- >> 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 -~----------~----~----~----~------~----~------~--~---
hei
2008-Feb-20 12:57 UTC
Re: Strange thing with controller''s helper method and method return value
emm... Just basic knowledges... I almost spent two hours on it ... Thank you very much~~~ On Feb 20, 8:51 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 20 Feb 2008, at 12:41, hei wrote: > > > > > > > > > On Feb 20, 4:32 pm, Visit Indonesia 2008 <rails-mailing-l...@andreas- > > s.net> wrote: > >>> 1, why my helper method does not work > >>> I hava a controller named msg, and i define a method test(id) > >>> in / > >>> app/helpers/msg_helper.rb > >>> but i can not use this method in /app/controllers/ > >>> msg_controller.rb. > >>> Can anyone tells me why ?Doedn''t rails require msg_helper.rb for > >>> me ? > > >> A helper is simply a module containing methods that assist a view not > >> your controller. If you want make method for your controller and tobe > >> used to many controller you can put your method in application.rb > > > See a content I copy from "Agile Web Development with Rails 2nd" 18.1 > > Context and Dependencis > > Rails handles many configuration dependencies automatically; as a > > developer > > you can normally rely on it to do the right thing. For example, if a > > request arrives forhttp://my.url/store/list, Rails will do the > > following. > > 1. Load the file store_controller.rb in the directory app/ > > controllers. (This > > loading takes place only once in a production environment). > > 2. Instantiate an object of class StoreController. > > @@@ 3. Look in app/helpers for a file called store_helper.rb. If > > found, it is loaded > > and the module StoreHelper is mixed into the controller object. > > 4. Look in the directory app/models for a model in the file store.rb > > and > > load it if found. > > That''s just wrong - it''s never been like that. > > > > > Do you see 3 ? is rails 2.0.2 changes this ? > >>> and i find xx i get via xx = test(id) is nil > > >> Sorry I not understand what do you mean on sentences above. > > > I mean , test(id) is supposed to renturn a hash with value {:text => > > 1} > > but actually , it returns nil or {} > > You can have a simple test > > Add this to one of your controller > > def test_action > > new = {} > > value = test_method > > new.merge(value) > > merge doesn''t alter the hash, it returns a new hash with the merge > contents (merge! does an inplace merge) > > Fred > > > > > > > render :text => new[:text] > > end > > :private > > def test_method() > > value = {:text => 1} > > return value > > end > > Then visithttp://root/controller/test_actionto see what it returns > > Actually, ''value'' returned by test_method is something like > > > flash: !map:ActionController::Flash::FlashHash > > :value: &id001 > > :text: 1 > > :value: *id001 > > I think new.merge(value) makes this > > Can anyone tells me more details ? > > Thanks! > >> ~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~* > >> Reinhart Ariando > >> YM : booking2heaven > >> Web:http://teapoci.blogspot.com > >> ~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~* > >> -- > >> Posted viahttp://www.ruby-forum.com/.- Hide quoted text - > > - Show quoted text -- Hide quoted text - > > - Show quoted text ---~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---