Topic: numericality validator breaks ajax code

hello all,

I have a very strange issue with the numericality validator.

The javascript code works greatly: On the index page I can create new articles that automatically adds itself to the article list. Validation with jquery plugin also works.

But I added

validates :pricePerUnit, :numericality => greater than 0 (pseudocode)

Which does work when javascript is disabled on browser.

But my ajax create.js does not work anymore. It gives me a long error messages, that does not make any sense to me.

Started POST "/articles" for 127.0.0.1 at 2012-03-17 10:56:33 +0100
  Processing by ArticlesController#create as JS
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"Fj7oou2r913sshIROE8RLqJTbF6G25D7PNu9Zqznwl4=", "article"=>{"article"=>"baaaaaass", "unit"=>"ff", "pricePerUnit"=>"0", "supplier_id"=>"1"}, "commit"=>"Create Article"}
   (0.0ms)  BEGIN
   (0.0ms)  ROLLBACK
  Supplier Load (0.0ms)  SELECT `suppliers`.* FROM `suppliers` WHERE `suppliers`.`id` = 1 LIMIT 1
Rendered articles/_article.html.erb (19.0ms)
Rendered articles/create.js (20.0ms)
Completed 500 Internal Server Error in 44ms

ActionView::Template::Error (No route matches {:action=>"edit", :controller=>"articles", :id=>#<Article id: nil, article: "baaaaaass", unit: "ff", pricePerUnit: #<BigDecimal:6aeb6d0,'0.0',4(8)>, supplier_id: 1, created_at: nil, updated_at: nil>}):
    4:   <td class="price_col"><%= number_to_currency(article.pricePerUnit, :unit => '&euro;', :format => '%n %u',:separator => ",")%></td>

    5:   <td><%= article.supplierName %></td>

    6:   <td><%= link_to 'Show', article %></td>

    7:   <td><%= link_to 'Edit', edit_article_path(article) %></td>

    8:   <td><%= link_to 'Destroy', article, confirm: 'Are you sure?', method: :delete, :remote => true, :class => 'delete_article' %></td>

    9: </tr>
  app/views/articles/_article.html.erb:7:in `_app_views_articles__article_html_erb___151628410_36425256'
  app/views/articles/create.js:1:in `_app_views_articles_create_js__1011029850_37523112'
  app/controllers/articles_controller.rb:38:in `create'

Rendered c:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/actionpack-3.1.3/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (1.0ms)


Started POST "/articles" for 127.0.0.1 at 2012-03-17 10:56:37 +0100
  Processing by ArticlesController#create as JS
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"Fj7oou2r913sshIROE8RLqJTbF6G25D7PNu9Zqznwl4=", "article"=>{"article"=>"baaaaaass", "unit"=>"ff", "pricePerUnit"=>"0", "supplier_id"=>"1"}, "commit"=>"Create Article"}
   (0.0ms)  BEGIN
   (0.0ms)  ROLLBACK
  Supplier Load (0.0ms)  SELECT `suppliers`.* FROM `suppliers` WHERE `suppliers`.`id` = 1 LIMIT 1
Rendered articles/_article.html.erb (20.0ms)
Rendered articles/create.js (21.0ms)
Completed 500 Internal Server Error in 36ms

ActionView::Template::Error (No route matches {:action=>"edit", :controller=>"articles", :id=>#<Article id: nil, article: "baaaaaass", unit: "ff", pricePerUnit: #<BigDecimal:6a61820,'0.0',4(8)>, supplier_id: 1, created_at: nil, updated_at: nil>}):
    4:   <td class="price_col"><%= number_to_currency(article.pricePerUnit, :unit => '&euro;', :format => '%n %u',:separator => ",")%></td>

    5:   <td><%= article.supplierName %></td>

    6:   <td><%= link_to 'Show', article %></td>

    7:   <td><%= link_to 'Edit', edit_article_path(article) %></td>

    8:   <td><%= link_to 'Destroy', article, confirm: 'Are you sure?', method: :delete, :remote => true, :class => 'delete_article' %></td>

    9: </tr>
  app/views/articles/_article.html.erb:7:in `_app_views_articles__article_html_erb___151628410_36425256'
  app/views/articles/create.js:1:in `_app_views_articles_create_js__1011029850_37523112'
  app/controllers/articles_controller.rb:38:in `create'

Rendered c:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/actionpack-3.1.3/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (1.0ms)

thanks in advance timmy

Last edited by TimmyX (2012-03-17 06:11:51)

Re: numericality validator breaks ajax code

forgot to mention that I only get the error when the numericality validation fails.

but shouldnt it just ignore all the rails validation code, when using ajax calls?

Re: numericality validator breaks ajax code

omg i am so stupid.

the problem was that create.js was still called even when the new article was not saved. (@article.save).