How about something like this? Don't try to rescue exception, instead avoid 
it by checking if valid? before attempting save.

class Song < Sequel::Model
  plugin :validation_helpers

  def validate
    super
    validates_presence [:title, :length, :released_on, :lyrics]
  end
end

get '/new' do
  @song = Song.new
  slim :new_song
end

post '/' do
  @song = Song.new(params[:song])

  if @song.valid?
    @song.save
    flash[:notice] = "Song successfully added #{@song}"
    redirect to("/#{@song}")
  else
    flash[:notice] = @song.errors.full_messages.join("\n")
    redirect to("/new")
  end
end



FYI, there's actually a better way than redirecting to "/new", and that is 
just rendering out the "new_song" template again if saving fails. That 
preserves error messages which you can then output beside each input (if 
you wanted to). Likely something to work towards since I believe you're 
following a book? and no point in complicating things too much.

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.

Reply via email to