Thank you for the reply Adam, 

First - a method block is an implicit begin/end block, so if you are 
> immediately inside a method definition, you can skip begin/end and just use 
> rescue


Ok, did not know that (still learning Ruby). I did resolve the issue, or at 
least it is working correctly (no tests yet).  Anyway, here is my newer 
code.  I'm still wondering why if in get '/new' I have a new instance 
created  that in def create_song, I wasn't able to use `set` and then 
`save`.   Make sense what I'm asking?  

def create_song
    begin
      @song = Song.new(params[:song]).save
    rescue 
    end
  end

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


post '/' do
  protected!
  if create_song
      flash[:notice] = "Song successfully added #{@song}"
      redirect to("/#{@song}") 
    else flash[:notice] = "#{@song.errors.full_messages.join("\n")}"
      redirect to("/new")
  end 
end

I'm not sure what is Sequel::ValidationFailed  .  I found though that by 
just using `rescue` in def create_song things flowed back correctly to the 
post route. I'll take the begin statement out.

Finally model code, I did not add true to the raise statement so maybe 
create will work if I add it.


class Song < Sequel::Model
  plugin :validation_helpers
  self.raise_on_save_failure


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


On Tuesday, October 17, 2017 at 7:47:00 AM UTC-6, Adam Daniels wrote:
>
>
>
>>
>>
> Just a few thoughts here - I cant' tell if you resolved this or not.
>
> First - a method block is an implicit begin/end block, so if you are 
> immediately inside a method definition, you can skip begin/end and just use 
> rescue.
>
>     def create_song
>       @song = Song.create(params[:song])
>     rescue Sequel::ValidationFailed
>       flash[:notice] = @song.errors.full_messages
>     end
>
> Second, raise_on_save_failures likely needs a true or false assignment to 
> it, and I believe the default is true, so that line in your model is likely 
> a no-op.
>
>     self.raise_on_save_failure = true
>
> Third, we don't see where the create_song method is being called, so it's 
> probably difficult for most of us to provide any valuable feedback in that 
> regard. I'm assuming you have a post route that handles it.
>

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