Rails 3. Leaving the wrong value for integer attributes on the form

I have a ' Man ' model , and this model has an integer attribute : age .
For example, I use validation for this integer attribute : age .

When I add a new ' Man ' with the wrong value : age (for example, "2aaa3" ), it returns me with an error, but it is also cut: age type "2" . But I do not want this. I want to leave the last invalid value "2aaa3" .

So the question is, “How can I do this?”

+3
source share
2 answers

Hello to everyone who is interested in my question.

I am glad to say that I myself created the solution. But I have a few comments. )))

First, the solution:

class ApplicationController < ActionController::Base
...
...
  after_filter :restore_int_value, :only => [:create, :update]
...
...
  private
...
...
  def restore_int_value
    response.body = response.body.gsub(/(numeric.*<input id=")([^_]*)(_)([^"]*)(.*value=")(\d+)(")/){$1 + $2 + $3 + $4 + $5 + params[$2][$4] + $7}
  end
...
...
end

Secondly, the comments:

1) The solution runs along gems such as Formtastic and Simple_form. These gems build forms unwound in a detailed html and enable us to use a regular expression. If you don’t use such gems, you can simply wrap all your integer attributes, for example, in the 'p' tag with the class 'numeric', as shown below, and I think my solution will work for this too:

<p class="numeric"> <%= text_field(:human, :age) %> </p>

2) , . ( ​​ "Handle Multiple Models in One Form" "Advanced Rails Recipes" ). , , JavaScript. , , JavaScript. JavaScript ( ). .;) JavaScript, , ( , ). , .;)

, , , -, .;)

0

Rails. , . javascript, , validates_numericality.of.

0

Source: https://habr.com/ru/post/1773313/


All Articles