DEV Community

Alexander Rovang
Alexander Rovang

Posted on • Edited on

Sinatra Project for Flatiron

Oh, Frankie. Still light as a feather.

After 2 months of the mental gymnastics that always seem to accompany a new skill, we FINALLY got to work on something that has the look of a real live website!

It's nothing fancy, but it WORKS. And that is pretty amazing.

My project was for Art Collectors. The intention was to have a place where a Collector could store information about their collection and browse through the names of artists and artworks that were in other people's collections as well (although with a more limited scope).

From a technical aspect, this project seemed fairly straightforward at the beginning. Create your ActiveRecord Migrations, set up the associations in your Models, map out the various CRUD routes in your controllers, and then add a little bit of CSS magic to doll the whole thing up.

For me, where it started getting interesting was when I began thinking about what I as a USER would want from the website. As I began to wander around the localhost:9393, I kept finding myself thinking "Hm. I wish I could edit something on this page." And then I would go back into the code and see how that desire could be achieved. It was crazy to see how many different parts of the code needed to be addressed by a single change on the website. And a good reminder that as interesting as coding is, the user experience is really the focus of this work.

A good example of this was when I tried to delete an Artist for the first time. Getting rid of the artist was easy, but suddenly there were artworks with no owner! Solved this via a block that also deleted artworks when their owner was gone.

  delete '/artists/:id/delete' do #destroy
    if !logged_in?
      redirect '/login'
    else
      artist = Artist.find_by_id(params[:id])

      if artist.collector == current_user
        art = artist.artworks 
        art.each do |work|
          work.delete
        end
        artist.delete
        redirect to "/artists"
      else
        redirect to "/artworks/error"
      end
    end
  end
Enter fullscreen mode Exit fullscreen mode

Another thing I found fascinating was the amount of work it took to validate a user. I have a suspicion that as we learn more at Flatiron there are going to be methods & filters that will make this process a lot simpler, but under the hood it's a lot of work! This is a snippet of my validation for a user to have a unique name and email:

post '/collectors' do

    if params[:name] == "" || params[:email] == "" || params[:password] == ""
      redirect "/signup"
    else
        if Collector.find_by(name: params[:name]) || Collector.find_by(email: params[:email])
          redirect "/signup"
        else
          @collector = Collector.new
          @collector.name = params[:name]
          @collector.email = params[:email]
          @collector.password = params[:password]
          if @collector.save
            redirect '/login'
          else
            erb :"/collectors/new"
          end
        end
      end
Enter fullscreen mode Exit fullscreen mode

First I had to check to ensure that they'd filled in all the fields, THEN I had to check the Collector class to see if the email or username that the person signing up had entered (via params[:name] and params[:email]) was already in use... THEN create a new collector with all the params and save it to the database!

Ruby, you and Sinatra make a lovely couple, but from now on we're splitting the bill. :)

Top comments (0)