In the example below, what is the dash doing between the class attribute and array:
(field_helpers - [:label, :check_box, :radio_button, :fields_for, :hidden_field, :file_field]).each do |selector|
In the example below, what is the dash doing between the class attribute and array:
(field_helpers - [:label, :check_box, :radio_button, :fields_for, :hidden_field, :file_field]).each do |selector|
In ruby a minus on an array removes elements. So its removing elements from the array.
Yeah so I would like to see the returned array of instance methods of field_helpers of FormBuilder. I try to return it in console:
1.9.3p0 :016 > FormBuilder.field_helpers NoMethodError: undefined method `field_helpers' for FormBuilder:Class
Why is it saying undefined method field_helpers when that method exists on the FormBuilder class:
class_attribute :field_helpers
ok this was oversight on my part. First of all, instead of using the helper helper in the console, you can directly access actionview just by specifying the constant. So ActionView::Helpers::FormBuilder.instance_methods would work. But what was oversight is that I was wondering that FormBuilder's field_helpers class method which returns an array of methods for the object was actually removing all the methods it defines. But if you look at its assignment, it is grabbing the methods of FormHelper, not FormBuilder. It does "FormHelper.instance_methods" and FormHelper is what defines the key methods that we often invoke on the form builder that have generic output:
ActionView::Helpers::FormHelper.instance_methods => [:convert_to_model, :form_for, :fields_for, :label, :text_field, :password_field, :hidden_field, :file_field, :text_area, :check_box, :radio_button, :search_field, :telephone_field, :phone_field, :url_field, :email_field, :number_field, :range_field]
The reason why we exclude :label, :check_box, :radio_button, :fields_for, :hidden_field, :file_field is because they have their own unqiue definition which is done in the form builder itself.
So whene we cal text_field, for example, that method was dybamically built using class_eval. and what it does is just call the method from selector.inspect on tempalte, and of course the tempalte is ActionView::Helpers::FormHelper, since that is also where form_for itself is defined. and in our view we invoke form_for, where self is ActionView with tha tmodule included.