I have a model, Work, that when I create a new instance with incorrect
field data does not call the validation methods. When I update an
existing instance with the same incorrect fields the validation
methods are called.

The create action fails on the save method and re-routes to the new
action, causing runtime errors.

I dont understand why update validates correctly and create doesn't.

Validation lines in the model are as follows:

validates :start, :presence => { :message => "must be a valid date/
time" }
validates :end, :presence => {:message => "must be a valid date/
time"}
validate :start_must_be_before_end_time

def start_must_be_before_end_time
errors.add(:start, "must be before end time") unless
self.start < self.end
end

Any pointers would be great.

--
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

  • Colin Law at May 31, 2012 at 8:54 pm

    On 31 May 2012 20:50, flaps wrote:
    I have a model, Work, that when I create a new instance with incorrect
    field data does not call the validation methods. When I update an
    existing instance with the same incorrect fields the validation
    methods are called.

    The create action fails on the save method and re-routes to the new
    action, causing runtime errors.
    The validation /should/ cause the save method to fail, that is the
    whole point of validation. Or do you mean that the runtime errors
    appear /during/ the save? If so then give us some more information
    about that (the full error message and stack trace).

    Colin
    I dont understand why update validates correctly and create doesn't.

    Validation lines in the model are as follows:

    validates :start, :presence => { :message => "must be a valid date/
    time" }
    validates :end, :presence => {:message => "must be a valid date/
    time"}
    validate :start_must_be_before_end_time

    def start_must_be_before_end_time
    errors.add(:start, "must be before end time") unless
    self.start < self.end
    end

    Any pointers would be great.

    --
    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.
    --
    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.
  • Flaps at Jun 1, 2012 at 3:31 am
    THanks for your response. The validation methods are not called on a
    create, allowing the save method to be called, unprotected. I dont
    want the save to be called let alone fail.

    The validation method should stop the save method from being run and
    redirect the user to the new action/view with errors displayed, right?

    The validation methods in the model are called on an update and errors
    reported back to the view. And I dont know why I am getting expected
    validating behaviour with an update but not with a create.

    I can post the error logs in the morning, but, on a create, they
    report a rollback, a 500 error, because the failed save call triggers
    the unexpected view to be routed.
    On May 31, 3:53 pm, Colin Law wrote:
    On 31 May 2012 20:50, flaps wrote:

    I have a model, Work, that when I create a new instance with incorrect
    field data does not call the validation methods. When I update an
    existing instance with the same incorrect fields the validation
    methods are called.
    The create action fails on the save method and re-routes to the new
    action, causing runtime errors.
    The validation /should/ cause the save method to fail, that is the
    whole point of validation.  Or do you mean that the runtime errors
    appear /during/ the save?  If so then give us some more information
    about that (the full error message and stack trace).

    Colin




    I dont understand why update validates correctly and create doesn't.
    Validation lines in the model are as follows:
    validates :start, :presence => { :message => "must be a valid date/
    time" }
    validates :end, :presence => {:message => "must be a valid date/
    time"}
    validate :start_must_be_before_end_time
    def start_must_be_before_end_time
    errors.add(:start, "must be before end time") unless
    self.start < self.end
    end
    Any pointers would be great.
    --
    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 athttp://groups.google.com/group/rubyonrails-talk?hl=en.
    --
    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.
  • Colin Law at Jun 1, 2012 at 6:47 am
    On 1 June 2012 04:31, flaps wrote:

    Please don't top post, it makes it difficult to follow the thread.
    Insert your reply into previous message at appropriate points. Thanks
    THanks for your response. The validation methods are not called on a
    create, allowing the save method to be called, unprotected. I dont
    want the save to be called let alone fail.
    As I said previously, the validation methods will be called during the
    save, unless you are doing something to call them earlier. I suggest
    you post the contents of the create action in the controller so we can
    see what is going on.
    The validation method should stop the save method from being run and
    redirect the user to the new action/view with errors displayed, right?
    The validation methods will not stop save from being called unless you
    have included code in the create method to do this. Also any
    redirection is up to the code you have written.
    The validation methods in the model are called on an update and errors
    reported back to the view. And I dont know why I am getting expected
    validating behaviour with an update but not with a create.

    I can post the error logs in the morning, but, on a create, they
    report a rollback, a 500 error, because the failed save call triggers
    the unexpected view to be routed.
    Also post the relevant code.

    Colin
    On May 31, 3:53 pm, Colin Law wrote:
    On 31 May 2012 20:50, flaps wrote:

    I have a model, Work, that when I create a new instance with incorrect
    field data does not call the validation methods. When I update an
    existing instance with the same incorrect fields the validation
    methods are called.
    The create action fails on the save method and re-routes to the new
    action, causing runtime errors.
    The validation /should/ cause the save method to fail, that is the
    whole point of validation.  Or do you mean that the runtime errors
    appear /during/ the save?  If so then give us some more information
    about that (the full error message and stack trace).

    Colin




    I dont understand why update validates correctly and create doesn't.
    Validation lines in the model are as follows:
    validates :start, :presence => { :message => "must be a valid date/
    time" }
    validates :end, :presence => {:message => "must be a valid date/
    time"}
    validate :start_must_be_before_end_time
    def start_must_be_before_end_time
    errors.add(:start, "must be before end time") unless
    self.start < self.end
    end
    Any pointers would be great.
    --
    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 athttp://groups.google.com/group/rubyonrails-talk?hl=en.
    --
    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.
    --
    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 Jun 1, 2012 at 7:36 am

    On 1 June 2012 04:31, flaps wrote:
    THanks for your response. The validation methods are not called on a
    create, allowing the save method to be called, unprotected. I dont
    want the save to be called let alone fail.
    Is there any chance you could post your controller code. I wonder
    whether you've got a ".save false" in the create action...

    --
    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.
  • Martyn W. at Jun 4, 2012 at 3:24 pm

    Michael Pavling wrote in post #1062778:
    On 1 June 2012 04:31, flaps wrote:
    THanks for your response. The validation methods are not called on a
    create, allowing the save method to be called, unprotected. I dont
    want the save to be called let alone fail.
    Is there any chance you could post your controller code. I wonder
    whether you've got a ".save false" in the create action...
    Here is the create code in the controller:

    # POST /works
    # POST /works.json
    def create
    @work = Work.new(params[:work])
    @operations = @work.work_ticket.part.operations
    respond_to do |format|
    if @work.save
    format.html { redirect_to @work, notice: 'Work was successfully
    created.' }
    format.json { render json: @work, status: :created, location:
    @work }
    else
    format.html { render action: "new" }
    format.json { render json: @work.errors, status:
    :unprocessable_entity }
    end
    end
    end

    --
    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.
  • Martyn W. at Jun 4, 2012 at 3:28 pm

    Martyn W. wrote in post #1063043:
    Michael Pavling wrote in post #1062778:
    On 1 June 2012 04:31, flaps wrote:
    THanks for your response. The validation methods are not called on a
    create, allowing the save method to be called, unprotected. I dont
    want the save to be called let alone fail.
    Is there any chance you could post your controller code. I wonder
    whether you've got a ".save false" in the create action...
    Here is the create code in the controller:

    # POST /works
    # POST /works.json
    def create
    @work = Work.new(params[:work])
    @operations = @work.work_ticket.part.operations
    respond_to do |format|
    if @work.save
    format.html { redirect_to @work, notice: 'Work was successfully
    created.' }
    format.json { render json: @work, status: :created, location:
    @work }
    else
    format.html { render action: "new" }
    format.json { render json: @work.errors, status:
    :unprocessable_entity }
    end
    end
    end
    It looks like the validation is called, it is the render after the
    failed fail that errors out now. I get the following error in the
    browser:

    Routing Error

    No route matches {:controller=>"works"}

    Try running rake routes for more information on available routes.

    All the routes for Work are working, except in this case after a failed
    invalid save.

    --
    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.
  • Dasibre at Jun 3, 2012 at 3:30 am
    I have a model, Work, that when I create a new instance with
    incorrect
    field data does not call the validation methods. When I update an
    existing instance with the same incorrect fields the validation
    methods are called.
    The create action fails on the save method and re-routes to the new
    action, causing runtime errors.
    I dont understand why update validates correctly and create doesn't.
    Validation lines in the model are as follows:
    validates :start, :presence => { :message => "must be a valid date/
    time" }
    validates :end, :presence => {:message => "must be a valid date/
    time"}
    validate :start_must_be_before_end_time
    def start_must_be_before_end_time
    errors.add(:start, "must be before end time") unless
    self.start < self.end
    end
    Any pointers would be great.

    I think your (validation :start, :presence )
    seems to be missing the "true" option.

    validation :start, :presence => true, :message => "must be a
    valid......
    On May 31, 3:50 pm, flaps wrote:
    I have a model, Work, that when I create a new instance with incorrect
    field data does not call the validation methods. When I update an
    existing instance with the same incorrect fields the validation
    methods are called.

    The create action fails on the save method and re-routes to the new
    action, causing runtime errors.

    I dont understand why update validates correctly and create doesn't.

    Validation lines in the model are as follows:

    validates :start, :presence => { :message => "must be a valid date/
    time" }
    validates :end, :presence => {:message => "must be a valid date/
    time"}
    validate :start_must_be_before_end_time

    def start_must_be_before_end_time
    errors.add(:start, "must be before end time") unless
    self.start < self.end
    end

    Any pointers would be great.
    --
    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.
  • Frederick Cheung at Jun 4, 2012 at 9:30 am

    On Jun 3, 4:30 am, dasibre wrote:
    I have a model, Work, that when I create a new instance with
    incorrect
    Why not post your controller code as several people have asked rather
    than reposting your original question.

    Fred


    field data does not call the validation methods. When I update an
    existing instance with the same incorrect fields the validation
    methods are called.
    The create action fails on the save method and re-routes to the new
    action, causing runtime errors.
    I dont understand why update validates correctly and create doesn't.
    Validation lines in the model are as follows:
    validates :start, :presence => { :message => "must be a valid date/
    time" }
    validates :end, :presence => {:message => "must be a valid date/
    time"}
    validate :start_must_be_before_end_time
    def start_must_be_before_end_time
    errors.add(:start, "must be before end time") unless
    self.start < self.end
    end
    Any pointers would be great.

    I think your (validation :start, :presence )
    seems to be missing the "true" option.

    validation :start, :presence => true, :message => "must be a
    valid......

    On May 31, 3:50 pm, flaps wrote:






    I have a model, Work, that when I create a new instance with incorrect
    field data does not call the validation methods. When I update an
    existing instance with the same incorrect fields the validation
    methods are called.
    The create action fails on the save method and re-routes to the new
    action, causing runtime errors.
    I dont understand why update validates correctly and create doesn't.
    Validation lines in the model are as follows:
    validates :start, :presence => { :message => "must be a valid date/
    time" }
    validates :end, :presence => {:message => "must be a valid date/
    time"}
    validate :start_must_be_before_end_time
    def start_must_be_before_end_time
    errors.add(:start, "must be before end time") unless
    self.start < self.end
    end
    Any pointers would be great.
    --
    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 31, '12 at 7:50p
activeJun 4, '12 at 3:28p
posts9
users6
websiterubyonrails.org
irc#RubyOnRails

People

Translate

site design / logo © 2022 Grokbase