then, in creating a new Contribution DB, I need to check the user input
values. Of course, if the values are not valid, it needs to stay in the
same page showing error messages .
in ‘contributions_controller.rb’,
respond_to do |format|
if @contribution.save
format.html { render action: "new", notice: 'Contribution was
hoping that the ‘save’ method FAILS upon inputing string values, for
example.
But interestingly, if I input, say, ‘this’ in the input form, it directs
to the next page and the ‘0’ (an integer, though) value is inserted in
the DB. I do not have any default value to ‘price’.
Could anyone point out any mistakes that I am making here?
But interestingly, if I input, say, ‘this’ in the input form, it directs
to the next page and the ‘0’ (an integer, though) value is inserted in
the DB. I do not have any default value to ‘price’.
If you pop open a Rails console and type:
‘this’.to_i
What result do you get?
Yep - zero is an integer. Rails casts the values from params to map to
the DB fields, so if you want validate the values the user typed
rather than the values Rails has translated them to, you may need to
write a custom validation and check the “before_typecast” value.