Ruby on Rails Sunday, July 27, 2014

I made helper ​​for Bootstrap Form. In it I made ​​a separate method has_error?, who must determine whether the error messages, but this is not happening. How can I fix it? If there are still errors, omissions, and not the optimal code will once remarks and comments as to optimize (because I just learning ROR)

Here is the method itself has_error? (a piece of code from the file app/helpers/bootstrap_control_helper.rb: 93)

...   def has_error?(obj, method)     obj.respond_to?(:errors) && !(method.nil? || obj.errors[method].empty?)   end   ...
Here is the full text of the file app/helpers/bootstrap_control_helper.rb:
module BootstrapControlHelper     attr_reader :object, :template, :options       COMMON_OPTIONS = [:layout, :label_class, :label_col, :label_offset, :label_text, :control_class, :control_col,                        :control_offset, :invisible_label, :placeholder, :popover, :error_disable, :row_disable, :required]       LABEL_OPTIONS = [:layout, :label_class, :label_col, :label_text, :label_offset, :invisible_label]       CONTROL_OPTIONS = [:layout, :control_class, :control_col, :control_offset]       FIELD_HELPERS = %w{color_field date_field datetime_field datetime_local_field     email_field month_field number_field password_field phone_field     range_field search_field telephone_field text_area text_field time_field     url_field week_field}       DATE_SELECT_HELPERS = %w{date_select time_select datetime_select}       def self.create_tagged_field(method_name)       define_method(method_name) do |obj, method, options = {}|         generate_form_group(obj, method, options) do           super(obj, method, options.except(*COMMON_OPTIONS))         end       end     end       FIELD_HELPERS.each do |method_name|       create_tagged_field(method_name)     end       def generate_form_group_row(options = {})       options[:control_col] ? content_tag(:div, "", class: "row") { yield } : yield     end       def generate_form_group(obj, method, options = {}, &block)       generate_form_group_row(options.slice(:control_ col)) do         if options[:control_col] && options[:layout].nil?           options[:class] = [options[:class], grid_system_class(options[:control_col])].compact.join(" ")         end         options[:class] = [options[:class], "form-group"].compact.join(" ")         options[:class] = [options[:class], "has-error"].compact.join(" ") if has_error?(obj, method)         content_tag(:div, class: options[:class]) do           generate_label(obj,                           method,                           options. slice(*LABEL_OPTIONS)).concat(generate_control(options, &block))         end       end     end       def generate_label(obj, method, options={})       options[:class] = label_class(options)       label(obj, method, options[:label_text], options.slice(:class))     end       def label_class(options={})       options[:label_class] = [options[:label_class], "sr-only"].compact.join(" ") if options[:invisible_label]       options[:label_class] = [options[:label_class], "control-label"].compact.join(" ") if options[:layout] == :horizontal       [label_col_class(options), options[:label_class]].compact.join(" ")     end       def label_col_class(options={})       unless options[:label_col]         if options[:layout] == :horizontal           grid_system_class(defau lt_horizontal_label_col)         elsif options[:layout] == :inline           "sr-only"         end       else         options[:label_col]       end     end       def generate_control(options={}, &block)       options[:class] = control_class(options)       if options[:layout] == :horizontal         content_tag(:div, class: control_col_class(options)) { yield }       else         yield       end     end       def control_class(options={})       ["form-control", options[:control_class]].compact.join(" ")     end       def control_col_class(options={})       unless options[:control_col]         grid_system_class(default_horizonta l_control_col) if options[:layout] == :horizontal       else         grid_system_class(options[:control_ col])       end     end       def has_error?(obj, method)       obj.respond_to?(:errors) && !(method.nil? || obj.errors[method].empty?)     end       def grid_system_class(col=default_control_col)       "col-#{default_grid_system}-#{col}" if (1..12).include?(col)     end       def grid_system_offset_class(col=default_offset_control_col)       "col-#{default_grid_system}-offset-#{col}" if (1..12).include?(col)     end       def default_offset_control_col       3     end       def default_horizontal_label_col       3     end       def default_control_col       12     end       def default_horizontal_control_col       7     end       def default_date_col       4     end       def default_grid_system       "sm"     end       def error_class       "has-error"     end   end
Here is the full text of the file app/helpers/bootstrap_form_helper.rb:
module BootstrapFormHelper       def bootstrap_form_for(object, options={}, &block)         layout = case options[:layout]                   when :inline                     "form-inline"                   when :horizontal                     "form-horizontal"                 end         if layout         options[:html] ||= {}         options[:html][:class] = [options[:html][:class], layout].compact.join(" ")       end         form_for(object, options, &block)     end   end
Here is the full text of the file app/helpers/bootstrap_builder_helper.rb:
module BootstrapBuilderHelper     class BootstrapBuilderHelper < ActionView::Helpers::FormBuilder # NestedForm::Builder       include BootstrapControlHelper       include BootstrapFormHelper     end   end
And the form in which I test app / views / sessions / new.html.erb:
...   <%= bootstrap_form_for(:session, url: sessions_path, html: { role: "form" }) do |session_fields| %>       <%= session_fields.text_field(:email, control_col: 8) %>       <%= session_fields.password_field(:password, control_col: 8) %>   <% end %>   ...






--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/189e630b-4daf-4104-865e-4db4f5986056%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment