Rails 3.1.3

I have a table 'Contribution' having a column, 'price' which must be
integers.

validate :price => true, :numericality => { :only_intger => true }

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
successfully created.' }
format.json { render json: @contribution, status: :created,
location: @contribution }
else
format.html { render action: "index" }
format.json { render json: @contribution.errors, status:
:unprocessable_entity }
end
end

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?

Thanks in advance.

soichi

--
Posted via http://www.ruby-forum.com/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Search Discussions

  • Michael Pavling at May 4, 2012 at 7:25 am

    On 4 May 2012 08:16, Soichi Ishida wrote:
    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.

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Soichi Ishida at May 4, 2012 at 8:41 am
    Thanks for your reply.
    If you pop open a Rails console and type:

    'this'.to_i

    What result do you get?

    1.9.3-p0 :008 > 'this'.to_i
    => 0
    1.9.3-p0 :009 >


    It's zero...it does not seem good at all ;)

    soichi

    --
    Posted via http://www.ruby-forum.com/.

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Michael Pavling at May 4, 2012 at 8:53 am

    On 4 May 2012 09:41, Soichi Ishida wrote:
    1.9.3-p0 :008 > 'this'.to_i
    => 0
    1.9.3-p0 :009 >

    It's zero...it does not seem good at all ;)
    That's a different thread entirely ;-)

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Juhyeong at May 4, 2012 at 9:07 am
    Hi All

    I am a newbie to this community.

    I recently published a gem called event_watcher for one of my project

    http://github.com/zzurang/event_watcher

    It is a simple dsl thing that is designed to help monitor certain function calls

    I wonder if I can get some feedback or advise in general



    Thanks


    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Matt Jones at May 4, 2012 at 12:04 pm

    On Friday, 4 May 2012 03:16:32 UTC-4, Ruby-Forum.com User wrote:
    Rails 3.1.3

    I have a table 'Contribution' having a column, 'price' which must be
    integers.

    validate :price => true, :numericality => { :only_intger => true }
    I'm not sure what effect this syntax will actually have, but it's not the
    one you're looking for. The line in your model should look like this

    validates :price, :numericality => { :only_integer => true }

    (note the plural 'validates' rather than 'validate' - they do two quite
    different things!)

    --Matt Jones

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/qR8jceMa0_MJ.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Soichi Ishida at May 5, 2012 at 4:02 am

    validates :price, :numericality => { :only_integer => true }

    (note the plural 'validates' rather than 'validate' - they do two quite
    different things!)

    I must be blind! It should have been

    validates :price, :presence => true, ...

    and plural, yes, it reminds me Rails like plural nouns.

    Thanks.

    soichi

    --
    Posted via http://www.ruby-forum.com/.

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedMay 4, '12 at 7:16a
activeMay 5, '12 at 4:02a
posts7
users4
websiterubyonrails.org
irc#RubyOnRails

People

Translate

site design / logo © 2021 Grokbase