Forum: Ruby on Rails Custom Error validation

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
David (Guest)
on 2009-04-22 09:17
(Received via mailing list)
I have a couple of concerns here. Should I only use model methods to
return objects and then save those objects in the controller? or is it
okay to save within a model method like below? The other thing is I
have created a javascript variable to flag if a time gets selected
from a custom javascript pull down menu and I would like to be able to
check that a time is selected and then display the error using rjs.
Below is what I have so far, it does not totally work, but I wanted to
get some input before I went any further.

def create
    case params[:event][:kind]
    when "appointment"
      event = Appointment.create_and_save(blah)
    when "personal"
      event = Personal.create_and_save(blah)
    end
  end

class Appointment < Event
  def self.create_and_save(blah)
    if time_selected == 'false'
      appointment = Appointment.new
      appointment.errors.add('start_int', "You must select a time.")
    else
    event_info = { blah }
    appointment = Appointment.new(event_info)
    end
  end
end

#now back in the controller i would like to use rjs to render this
error
#something like:
    respond_to do |format|
      if event.save!
        ...
      else
        format.js do
          render :update do |page|
            page.replace_html 'error', @user.return_validation_error
('start_int') if @user.errors['start_int']
            #page << "$('email_help').className = 'signup_error';" if
@user.errors['email']
          end
        end
      end
    end
This topic is locked and can not be replied to.