Giter VIP home page Giter VIP logo

sinatra-activerecord-using-tux's Introduction

Using Tux in Sinatra with ActiveRecord

Overview

In this lesson, we'll cover Tux, a Ruby gem, that you can use to make sure your database and ActiveRecord associations are set up properly.

Objectives

  • Describe the benefits of Tux
  • Set up Tux locally
  • Create, edit, delete, and update data

What Is Tux

Tux is an incredible Ruby gem that lets you access your database and perform all CRUD operations on it through the terminal. It also loads a full environment in the console that allows you to see all routes and views. Primarily, you'll use Tux to make sure your database is set up properly, play around with Ruby objects, and make sure your ActiveRecord associations are working properly.

For example, in this repo there is a simple Sinatra app with a User model and a method called say_name. I want to test that the say_name method works. Wouldn't it be great if we could enter into some sort of playground environment where we can test out our code?

In the past, we've used IRB for just that. Let's try dropping into IRB and testing out our code.

Open up IRB in terminal and require the file models/user.rb:

require models/user.rb

Oh no! We get an error that it doesn't recognize ActiveRecord. Let's try to require the ActiveRecord gem:

require activerecord

Trying to test our User model and methods in IRB is a big big mess. We keep running into issues. This is why Tux exists. By using the Tux gem, we can easily create objects, and test our methods to manipulate those objects.

Setup

Setting up Tux is fairly simple. All you need to do is include it in your Gemfile and run bundle install in terminal.

Using Tux

We've got a full Sinatra application with a single User class set up. We've already created the migration for you, but make sure you actually run the migration to create the user table.

Next, it's time to use Tux. In terminal in the directory of this walk-through enter tux. You should see something like this:

tux screenshot

The tux console has now loaded. Regular terminal commands won't work at this point, but you can use Ruby and ActiveRecord methods.

Create

Just like in our controller action, we can create a user.

user = User.create(:name => "Trisha", :email => "[email protected]", :fav_icecream => "mint chocolate chip")

Or:

user = User.new
user.name = "Beth"
user.email = "[email protected]"
user.fav_icecream = "rocky road"
user.save

Edit

We can edit a user that's already been saved to the database. Let's edit the first user.

user = User.first
user.name = "Trisha Yearwood"
user.save

Delete

Now let's delete the first user:

user = User.first
user.delete

Search for Specific Users

All the find methods work in Tux too!

user = User.find_by_id(2)
user = User.find_by(:name => "Beth")
user = User.first
user = User.last

Once you're done, just exit Tux by entering exit.

sinatra-activerecord-using-tux's People

Contributors

annjohn avatar devinburnette avatar dfenjves avatar drakeltheryuujin avatar ihollander avatar maxwellbenton avatar pletcher avatar sophiedebenedetto avatar victhevenot avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

sinatra-activerecord-using-tux's Issues

Terminal screenshots show shotgun rather than IRB or Tux console

I noticed a couple of the screenshots on this lesson aren't showing the right material.

Screenshot #1 is supposed to show IRB and requiring /models/user.rb but actual screenshot shows a shotgun server loading.

Screenshot #3 is supposed to show opening the Tux console but actual screenshot shows a rackup app.rb command running.

wrong img link

Hi , the image link in this readme

is supposed to show what the terminal looks like once Tux is up and running. But instead it shows whaqt happens when you run "rackup app.rb".

this is confusing for someone who doesn't know what tux is supposed to look like. A new, accurate img link would be helpful!

errors in less

all of the pictures in the lesson show launching shotgun, should show tux and require errors

wrong sample picture

Please check this file for the correct screenshot in the "Using Tux" section:

Thanks!

Tra

img issue

Within the heading 'using tux' there is a picture that is meant to demonstrate what the student is supposed to see after they enter 'tux' into their console within the context of this directory. I think that picture could be updated because it, instead, shows the student what they would see if they entered 'rackup app.rb' into the console.

issue with gem dependencies

Hi - I ran into a similar issue yesterday when trying to run a lab. Had to talk to a tech person. Something is going on in the file in the gem dependencies, had to change line 3 from:
gem 'activerecord', :require => 'active_record'
to
gem 'activerecord', '4.2', :require => 'active_record'

in order for tux to work. Otherwise there's a huge error:
NoMethodError: undefined method needs_migration?' for ActiveRecord::Migrator:Class /usr/local/rvm/gems/ruby-2.3.1/gems/ripl-rack-0.2.0/lib/ripl/rack.rb:38:in eval'
/usr/local/rvm/gems/ruby-2.3.1/gems/rack-2.0.5/lib/rack/builder.rb:55:in instance_eval' /usr/local/rvm/gems/ruby-2.3.1/gems/rack-2.0.5/lib/rack/builder.rb:55:in initialize'
(eval):1:in new' (eval):1:in initialize'
/usr/local/rvm/gems/ruby-2.3.1/gems/ripl-rack-0.2.0/lib/ripl/rack.rb:38:in eval' /usr/local/rvm/gems/ruby-2.3.1/gems/ripl-rack-0.2.0/lib/ripl/rack.rb:38:in initialize'...

Hopefully it can be fixed so other students don't run into the same error. Thanks!

images wrong

the images displayed in this readme seem to have nothing to do with the explanations given. they show unrelated commands in the terminal, displaying results of those commands

"bundle install" fails with: requested: activerecord = 4.2 [but]...activerecord locked at 5.0.0.

I forked and then cloned the lab from GitHub then ran "bundle install" and received this error message:

You have requested:
activerecord = 4.2

The bundle currently has activerecord locked at 5.0.0.
Try running bundle update activerecord

I notice that the gemfile specifies AR 4.2 yet the gemfile.lock specifies 5.0.0. Running the suggested command foeced the gemfile.lock back to 4.2 and I seem to be up and running now.

Several issues before Tux would run

Hey there,

I couldn't get Tux to run when I initially ran the command after opening the lab. Here's what I had to do to get it to run (some with the help of a technical coach):

  • Delete the Gemfile.lock
  • Update the ActiveRecord gem version gem 'activerecord', '~> 5.2', '>= 5.2.3', :require => 'active_record'
  • Change the sqlite3 gem to gem 'sqlite3', '~> 1.3.7'
  • Change line 3 of config.ru to if ActiveRecord::Base.connection.migration_context.needs_migration?
  • Change line 1 of the migration to class CreateUsers < ActiveRecord::Migration[4.2]
  • Delete schema.rb and development.sqlite files

That seemed to get it to work, but the lab should probably be updated to reflect those changes for other students.

Best,
A

Wrong images displayed in README

The images for Screen+Shot+2015-11-19+at+9.46.10+AM.png and tux.png are inconsistent to what is explained in the accompanying text.

No local tests

The right rail has "Your local tests have not been run" but there are no specs in the directory, so there is no way to get a green light here.

Missing migration.

The lab's README.md reads:

"Starter Code

We've included two migration files, 20150916143412_create_posts.rb and 20150916143408_create_users.rb, which will create tables for posts and users, respectively."

but './db/migrate' only has one migration (20150916143408_create_users.rb) to create the "users" table. I'll try to make my own second migration for now, but seems like the README might need to be updated, or new migration be made.

Wrong Images

The images for this Readme are incorrect. It shows starting shotgun instead of tux.

wrong image being used for tux demo

After the following text, there should be an image of the tux command line interface instead of output from the rackup command:

Next, it's time to use Tux. In terminal in the directory of this walk-through enter tux. You should see something like this:

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.