Hi Rails regularly uses object.method invocation result to find the value For example, consider <%= text_field object, method %> In this case the result of an object.method invocation would be used to provide a value to the text field. Although abundantly helpful, i run into problems when the methods require an argument for invocation. As a simple example, if my object is a Hash, and my method is []; but since the result would be invoked through object.[] and not object[], i *have to* specify an argument for [] in a fashion not quiet supported <%= text_field object, [](:key) %> Any workarounds or tips?? thanks shodhan
shodhan wrote:> <%= text_field object, method %> > In this case the result of an object.method invocation would be used to > provide a value to the text field. > Although abundantly helpful, i run into problems when the methods > require an argument for invocation.You can make the same use of the method_missing method as that done by ActiveRecord to create both attribute methods and dynamic finders. For the hash example the dynamic methods would be something like value_for_key_<key>. Alternatively, just assemble form elements and their values using either lower-level helpers like text_field_tag, or your own helpers that build on these to handle parameter-taking methods. -- We develop, watch us RoR, in numbers too big to ignore.
Hi Mark well, i can definitely do with a dynamic method in my case. thanks for that; though i am pretty sure there are others that would have a similar problem (*the Hash was only an example*). shouldnt there be built in support for methods with arguments ?? shodhan Mark Reginald James wrote:> shodhan wrote: > >> <%= text_field object, method %> >> In this case the result of an object.method invocation would be used >> to provide a value to the text field. >> Although abundantly helpful, i run into problems when the methods >> require an argument for invocation. > > > You can make the same use of the method_missing method as that done > by ActiveRecord to create both attribute methods and dynamic finders. > For the hash example the dynamic methods would be something like > value_for_key_<key>. > > Alternatively, just assemble form elements and their values using > either lower-level helpers like text_field_tag, or your own helpers > that build on these to handle parameter-taking methods. >