One flaw of Rails
… is that it’s only relatively recently become popular so it can be hard finding advice on problems when programming. I’ve come across a couple of issues so far where I’ve posted on forums and it’s taken a day or two before you get any kind of response and a couple of times there just plain hasn’t been a response.
I think Ruby developers seem to gravitate more towards mailing lists however, so I may have to try using one of them instead.
I’m currently having trouble with the following issue. If you know of a solution, PLEASE let me know. Otherwise, if I ever find a solution I’ll try to post here for others’ reference.
- Dynamic class names
- I’ve found the answer to the first part of this (i.e. being able to instantiate a class where the class name is variable), you use:
- foo = object.const_get(”class_name”).new
- The second part is a problem caused by Rails controller ‘modules’ - i.e. where you have SomeName::SomeController which isn’t a constant. I thought I’d found the answer when I found this mention on Ruby Weekly News of a method class_name.constantize but I now get this error:
- Manage::UsersController is not a symbol
- Full description of the problem with code
- I’ve found the answer to the first part of this (i.e. being able to instantiate a class where the class name is variable), you use:
June 28th, 2006 at 6:51 pm
Perhaps this will help you:
http://redcorundum.blogspot.com/2006/05/kernelqualifiedconstget.html
June 28th, 2006 at 9:20 pm
Hmmm, this sounds like a challenge. I had quick thought just from my understanding how the constants work. The reason might be because that class is in a module you need to let it know what module it belongs to so in your case:
c = Object.const_get(ctrl).new
may need to be, say ctrl is in AdamsModule:
c = AdamsModule.const_get(ctrl).new
rather than Object, but off course the snag is you need to know its module, but also it may be an issue if your modules have parent modules, erk!
I may have some time later in the weekend to help you work this out, this is a stab in the dark for now. Let me know how you get on.
June 28th, 2006 at 10:19 pm
[…] Contact « One flaw of Rails […]
June 28th, 2006 at 10:20 pm
Thanks both of you - will have another go at it either later tonight or tomorrow, bit knackered at the moment thanks to this