Home Rails 4.2.6 validation takes effect only in console, but not in controller (from browser)
Reply: 0

Rails 4.2.6 validation takes effect only in console, but not in controller (from browser)

user1400
1#
user1400 Published in April 20, 2018, 6:06 pm

Have gone through several SO rails validation questions, but none is fitting.

My model:

class InquiryInteger < Inquiry
    validates :answer, numericality: { only_integer: true, greater_than: 0 }
end

Now in the rails console, the validation is working (means: the record is not being saved when instance contains a text:

2.3.3 :017 > i1.save
   (0.2ms)  BEGIN
  Inquiry Exists (0.4ms)  SELECT  1 AS one FROM "inquiries" WHERE ("inquiries"."question_id" IS NULL AND "inquiries"."session_id" = 'jsklkjf8') LIMIT 1
   (0.2ms)  ROLLBACK
 => false
2.3.3 :018 > i1.errors.full_messages
 => ["Answer is not a number"]
2.3.3 :019 > i1.answer
 => "aADDASDASD"
2.3.3 :020 >

My controller create-Action:

...
respond_to do |format|
  if @inquiry.save
    format.html { redirect_to pages_path(url: params[:url]), notice: 'Thanks for your vote.' }
    format.json { render action: 'show', status: :created, location: @inquiry }
  else
    format.html { render action: 'new' }
    format.json { render json: @inquiry.errors, status: :unprocessable_entity }
  end
...

The save method is letting strings through and are stored in the DB. I restarted the thin web server, so changes must have been loaded. Even in byebug, the save method is not showing any messages.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.310542 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO