Ruby on Rails Monday, July 28, 2014

This is along the lines of what Matt is referring to.

class DayValidator < ActiveRecord::Validator
 
def validate()
   
if YOUR_TEST_FOR_INVALIDITY(record.day)
      date
= Date::DAYNAMES[record.day]
      record
.errors[:day] << "already has a target #{date}"
   
end
 
end
end

class YourClass < ActiveRecord::Base
  validates_with
DayValidator
end




On Friday, July 25, 2014 9:21:08 PM UTC-6, Ricky Hopkins wrote:
I am trying to convert an integer to a DAYNAME in my validation message

validates_uniqueness_of :day, scope: :store, :message => "already has a target for #{Date::DAYNAMES[self.day]}."

This does not seem to work, I cannot access the day value.
The following displays the correct value

validates_uniqueness_of :day, scope: :store, :message => "already has a target for %{value}."

This throws an error:

validates_uniqueness_of :day, scope: :store, :message => "already has a target for #{Date::DAYNAMES[%{value}]}."


Any help would be Amazing!

--
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/968ab5ce-dce8-4aa4-8db0-bc96f6b9eeaa%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ruby on Rails



On Sunday, 27 July 2014 06:21:40 UTC-4, Nikolay Lipovtsev wrote:

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


"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 

--
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/37dd2872-101d-4b55-861f-d94a4af595d7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ruby on Rails



On Friday, 25 July 2014 23:21:08 UTC-4, Ricky Hopkins wrote:
I am trying to convert an integer to a DAYNAME in my validation message

validates_uniqueness_of :day, scope: :store, :message => "already has a target for #{Date::DAYNAMES[self.day]}."

This does not seem to work, I cannot access the day value.
The following displays the correct value

validates_uniqueness_of :day, scope: :store, :message => "already has a target for %{value}."

This throws an error:

validates_uniqueness_of :day, scope: :store, :message => "already has a target for #{Date::DAYNAMES[%{value}]}."



Using double quotes with #{} will *definitely* not do what you want; the interpolation will be attempted at class-load time, not when the string is needed.

The value passed to :message is handed off to I18n.translate, so the docs for that may be some help.

It may be easier (and clearer) to just define a custom validation function that does what you want.

--Matt Jones

--
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/88db0d14-0e46-40a2-92f6-bd57af88496f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.