On Monday, October 16, 2017 at 6:33:02 PM UTC-4, French Fry wrote:
>
> I've tried a number of things with no effect.  The call to validation 
> errors  is an  undefined method.  Can you please tell me what I am leaving 
> out? 
>
> And if it's okay to double up, since `def new` instantiates a new Song 
> object, I wanted to use `set` in `def create_song` and it would not work.  
> Right now, while it works (minus the validation error issues) it's 
> essentially creating a new Song object twice.  Originally I used insert, so 
> I replaced it with set, and then save, but no go.  Ok, thank you for the 
> help!
>
> class Song < Sequel::Model
>   plugin :validation_helpers
>   self.raise_on_save_failure
>
>   def validate
>     super
>     validates_presence [:title, :length, :released_on, :lyrics]
>   end
> end
>
> def create_song
>     begin
>       @song = Song.create(params[:song])
>     rescue
>     flash[:notice] = @song.errors.full_messages
>     end
>   end
> end
>
> get '/new' do
>   protected!
>   @song = Song.new
>   slim :new_song
> end
>
>
>
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