Hey Adam, thank for the help. I was thinking to change some of the route
code around.
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.
>
I learned how to do this in Rails and was thinking of it. Wanted to get
the errors working first, but yes, better way. I'll have to dig my code
out on that method.
Cheers!
On Tuesday, October 17, 2017 at 12:28:03 PM UTC-6, Adam Daniels wrote:
>
> 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.