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
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 nil
Sorry I not understand what do you mean on sentences above.
> 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!
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
emm... Just basic knowledges... I almost spent two hours on it ... Thank you very much~~~