object.errors not work in Bootstrap Form Helper

13 views
Skip to first unread message

Nikolay Lipovtsev

unread,
Jul 27, 2014, 6:21:40ā€ÆAM7/27/14
to rubyonra...@googlegroups.com

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 %> 
...






Matt Jones

unread,
Jul 28, 2014, 4:34:23ā€ÆPM7/28/14
to rubyonra...@googlegroups.com
"not working" is insufficient to determine what's happening here. Can you outline the steps you're taking and what isn't happening?

--Matt JonesĀ 

Nikolay Lipovtsev

unread,
Jul 29, 2014, 7:31:17ā€ÆAM7/29/14
to rubyonra...@googlegroups.com
When I fill in the form is not valid valuesā€‹ā€‹, then the procedureĀ generate_form_groupĀ should add to <div class = "form_group"> class "has-error". But this is not happening. I think that there is something wrong with the Modules and Classes and inheritanceĀ ActionView::Helpers::FormBuilder.

Š²Ń‚Š¾Ń€Š½ŠøŠŗ, 29 ŠøюŠ»Ń 2014Ā Š³., 0:34:23 UTC+4 ŠæŠ¾Š»ŃŒŠ·Š¾Š²Š°Ń‚ŠµŠ»ŃŒ Matt Jones Š½Š°ŠæŠøсŠ°Š»:
Reply all
Reply to author
Forward
0 new messages