Alder Green
2006-Apr-08 15:50 UTC
[Rails] ActionView::Helpers::FormHelper instance methods should take an object reference rather than an object_name.
Hi ActionView::Helpers::FormHelper methods (e.g. #text_field, #check_box...) take object_name as an argument. From this argument they infer a name that is supposed to contains the reference to the relevant object. I can''t see why the FormHelper methods were designed that way. Compared to simply passing them a reference, the current way seems too complicated and an invitation to problems or at least limitations. For example, if you call text_field("post", "title"), the method would look for a @post instance variable. Imagine you were using a local variable for storing the relevant object, instead of an instance variable. If text_field took a reference, you would just call it with text_field(post, :title). However, since it takes a string an infers a reference name, you now have to change your assigned names or at least create new aliases to your existing names. To sum up, I can''t see any advantages to the object_name approach, I don''t understand why FormHelper was initially designed that way, so I think it should be changed so as to be more convenient and flexible. What do you think? Regards, Alder
Wiebe Cazemier
2006-Apr-08 15:55 UTC
[Rails] Re: ActionView::Helpers::FormHelper instance methods should take an object reference rather than an object_name.
On Saturday 08 April 2006 17:50, Alder Green wrote:> Hi > > ActionView::Helpers::FormHelper methods (e.g. #text_field, > #check_box...) take object_name as an argument. From this argument > they infer a name that is supposed to contains the reference to the > relevant object. > > I can''t see why the FormHelper methods were designed that way. > Compared to simply passing them a reference, the current way seems too > complicated and an invitation to problems or at least limitations. > > For example, if you call text_field("post", "title"), the method would > look for a @post instance variable. Imagine you were using a local > variable for storing the relevant object, instead of an instance > variable. If text_field took a reference, you would just call it with > text_field(post, :title). However, since it takes a string an infers a > reference name, you now have to change your assigned names or at least > create new aliases to your existing names. > > To sum up, I can''t see any advantages to the object_name approach, I > don''t understand why FormHelper was initially designed that way, so I > think it should be changed so as to be more convenient and flexible. > What do you think? > > Regards, > AlderIt provides some useful additions. Like mass assigning with .attributes=, and the use of validations without worrying about anything. Look at my post on the list I made 2 minutes before you did, which explains a bit about validations. In situations where you encounter limitations, use the text_field_tag method instead.
Rimantas Liubertas
2006-Apr-08 16:02 UTC
[Rails] ActionView::Helpers::FormHelper instance methods should take an object reference rather than an object_name.
<...>> For example, if you call text_field("post", "title"), the method would > look for a @post instance variable. Imagine you were using a local > variable for storing the relevant object, instead of an instance > variable. If text_field took a reference, you would just call it with > text_field(post, :title). However, since it takes a string an infers a > reference name, you now have to change your assigned names or at least > create new aliases to your existing names.<...> ... or simply use text_field_tag instead of text_field.> To sum up, I can''t see any advantages to the object_name approach, I > don''t understand why FormHelper was initially designed that way, so I > think it should be changed so as to be more convenient and flexible. > What do you think?Nothing should be changed, When you want to work with model''s attributes you use text_field (which makes updates as simple as update_attributes params[:model]) , when you want something ''flexible'' you use text_field_tag. Regards, Rimantas -- http://rimantas.com/