Life Is Short. Code Hard.

So that intense lab I was talking about yesterday… it took no time at all because of Rails. It was AWESOME. One of the things I learned today that is going to make life easier and ensure DRY.

# example_controller.rb

# Insert at top of controller
before_action :set_example, only: [:show, :edit, :update, :destroy]

# Insert at end of controller

def set_example
  @example = Example.find(params[:id])

So up until now to access a DB object as an instance variable I’ve used the ID passed in from the URL or form and then done a lookup to set the instance variable. That means my code was exactly the same and in four different methods. I always thought this was clunky but didn’t look too hard into how to change it. Now I have a way. So before_action is telling the controller before the action (method) happens to do this. However, only: for these [:show, :edit, :update, :destroy] actions. What do we need to do? That’s right, the :set_example action which is then accessible to the listed actions. So the instance variable is available for each of those views. Check out Filter on Rails Guides.

I also learned the difference between PATCH and PUT:

  • PUT has the ability to update the entire object.
  • PATCH updates only the elements that were changed. Therefore this is the preferred method because of lower overhead.

I realize it has only been 4 days since I started Rails but I like taking a snapshot of where I am and where I want to be on Sunday nights. According to my dashboard, I’m 32% ( 37 / 113 ) done with Rails. I must’ve counted wrong or things have been added because I counted 104 total plus 3 projects. Either way, I think I’m well on my way to hitting the projects starting May 10th which would take a <5 lessons/day pace.

Okay, I have my Sinatra Project review at 7:15am so I’m off to bed.

Time spent today: 3:52
Time spent total: 213:43
Lessons completed today: 12
Lessons completed total: 443

Fixing Problems Makes You Stronger

It was a LONG day on my computer. Ubuntu decided to suggest I update to 16.10 and I did. Well, that just started a string of issues that ended up in me reinstalling 16.04.2 LTS. I lost my entire morning and early afternoon to this process before I could get coding today. I’m sure I could’ve figured it out in time but time is something that is precious and I really had no need to upgrade. I’m on a LTS and things work which is all that I need.

In addition, Rails has presented some interesting problems that I’ve either figured out with Learn Expert help or on my own. Here are some examples, that I hope will help students behind me.

This error has persisted since before Rails but I never mentioned it (I don’t think):

An error occurred while installing json (1.8.3), and Bundler cannot
Make sure that `gem install json -v '1.8.3'` succeeds before bundling.

Figured out the best way to fix this is to delete the Gemfile.lock rm Gemfile.lock then bundle again bundle install and that install json 1.8.6 at this moment in time.

Now when you get this error:

/home/seth/.rvm/gems/ruby-2.4.1/gems/activesupport-4.2.5/lib/active_support/core_ext/numeric/conversions.rb:124:in `block (2 levels) in <class:Numeric>': stack level too deep (SystemStackError)

It’s a Rails issue. The easiest fix is to change the version of Rails in the Gemfile. I simply do gem 'rails', '5.0.2' then run bundle update and this error is fixed. I’ve gotten really good at nano Gemfile, do the edit quickly (rails is typically in the first 5 lines), then CTRL + X, Y, Enter, bundle install.

Another interesting error that has popped up on multiple occaisons is:

NoMethodError: undefined method `last_comment' for #< Rake::Application:0x007ff0cf37be38>

To fix this one another Gemfile edit must be done. Simply change, or add in, gem 'rake', '<11.0'. This is just an issue with rake past version 11. I didn’t read up on what it is exactly I just found this answer quickly and it works.

I do these things pretty regularly these days. I wish the Gemfiles of some of these lessons were scaled back some. There is so much unneeded stuff in them (I think). Also, a lot of the versions could be updated to take advantage of more recent versions getting rid of a lot of errors. I get a ton of DEPRECATION WARNING messages and they’re a pain.

On a learning note, while I thought form_tag was cool I had no idea about form_for when it comes to ActionView. Mind is sufficiently blown with how much more efficient this makes things and how much less coding needs to be done for the mundane form building. Also learning how to block CSRF attempts was an interesting bit of info. Cross-Site Request Forgery that is. One site making a request to another site via a form is the general flow of a CSRF.

These labs are getting intense too. The lab I’m currently on is equally or more complex than my Sinatra Portfolio Project which is saying something. Granted, I have been introduced to Rails generators so I won’t have to write EVERYTHING from scratch so we’ll see how it goes.

Time spent today: 5:08
Time spent total: 209:52
Lessons completed today: 11
Lessons completed total: 431

Rails Makes Life Easier but Difficult

Continuing with Rails. I now have 5 pages in my Cast Iron Design notebook. Something cool that I learned today:

<!-- How link_to works -->
<% @posts.each do |post| %>
  <div><%= link_to post.title, post_path(post) %></div>
<% end %>

<!-- Creates the following HTML -->
  <a href="/posts/1">My Title</a>

Which is pretty sweet considering how I was doing it in Sinatra. Something else that’s pretty sweet is that when we code post_path(post) Rails is smart enough to know that it needs to use the id attribute of the post object we just passed in. No need to write

Views in Rails also follow a convention of having a file extension of .html.erb whereas in Sinatra I was only using .erb for my views. Turns out Rails will render things right to left so first a file is having the embedded ruby rendered then it’s turned into HTML. It also appears that any plaintext will just be considered HTML.

I’ve also been learning a ton about how routes are handles in Rails. It seemed a little counter-intuitive compared to how I worked with Sinatra but as I keep learning and using it it is making much more sense.

I’m feeling stronger every day with in the future being able to pick up other frameworks based on the MVC structure. I’m getting a very low-level instruction and understanding of how things work and I know I’ll be a better developer because of it. Even if it takes longer to learn.

Time spent today: 2:20
Time spent total: 204:44
Lessons completed today: 4
Lessons completed total: 420

Fog is in the Brain

author: “Seth”
categories: [daily blog]
date: 2017-04-20T22:44:52-05:00
description: “Day 53 of Flatiron School”
featured: “fog.jpg”
featuredalt: “tree in fog”
featuredpath: “../../images/”

title: Fog is in the Brain

I don’t know what it is tonight but I just can’t get into the groove. I’m steady distracted and not focused. And no it’s not because of the date, you know you were thinking it. Ugh. At least it’s on a “short” day and I did attend a meetup tonight so I did something beneficial to my coding journey that took a few hours of my evening. I am a few pages into one of my notebooks though so I can say that’s good. Maybe I’ll look up the CORS spec instead of doing any more Rails learning tonight. It’s a topic I heard about at the beginning of the month but still haven’t read up on at all (I had to look up that link just now).

Time spent today: 1:11
Time spent total: 202:24
Lessons completed today: 4
Lessons completed total: 416

Yay! You’re on Rails!

Today I started Rails in all its glory. This will be my first full-on framework that I get to work with. From what I’ve seen though it’s one of those things that has so much the key will be to stay focused on what I’m trying to have Rails do instead of everything it can do. It’s crazy to think that after this large section Career Services will be reaching out to me to start that process while I finished up the JS part of the program. I have 104 lessons, labs, and lectures left before 3 projects. My goal is to be doing Rails projects by May 10th.

I started taking some more organized notes in my little notebook. We’ll see how that works out as we move forward. From looking over the curriculum authentication is the largest part of Rails so I’ll keep some good notes there. If I can keep myself from having to rewatch lecture videos I think the extra time writing things down will pay off.

On a software note. My favorite email client just had an upgrade and is available on Windows, Mac, and Linus now. Check out Nylas Mail.

Time spent today: 3:00
Time spent total: 201:13
Lessons completed today: 6
Lessons completed total: 412

I Deployed My Sinatra App To Heroku


You can say I’m pretty stoked. This blog is part of the requirements though so get ready :-). Actually, I didn’t take enough notes to really get into the code in this blog. I did hit some solid stumbling blocks and there is some major refactoring of code needing to happen but I’m overall pretty happy. A large part of the time I spent was making the app look good utilizing Bootstrap.

Something that definitely needs to be refactored is one of my helper methods:

def current_user_parents
  parents = []
  current_user(session).houses.each do |house|
    Parent.where(house_id: do |parent|
      parents << parent

It’s sandwich code (starts with a variable set to an empty array, fills that array, returns that array) so I know I can most likely eliminate two lines from that or even three if I’m not pushing onto that parents variable anymore. We’ll see. I’m interested to pick the brain of the instructor that goes over the project with me.

You can see what I’ll call v0.8 (I figure it’s close to v1.0 but not yet lol) at I feel like the code is solid but still some functionality and visual things that need to be resolved.

  • I’d like to add a static footer to the site.
  • If a house is deleted the parent and children remain in the database but can never be accessed again because of how I list them in relation to the house they’re in.
  • When there are no parents or children I need to add a link suggesting to create some.
  • When a user tries to access the info for something they didn’t create (ie. they created house 8 but try and go to the link for house 5) the error page tells them to log in even though they are logged in. I need to create a different error page for logged in users trying to access stuff that isn’t there’s vs. the error page when a person isn’t logged in.
  • Add some color!

I’m sure I could find more. Feel free to check out the app. Sign up, use it, and report issues to the GitHub link at the top right on every page of the application.

It was interesting deploying to Heroku as well. There isn’t a solid guide on deploying a Sinatra app with a DB to Heroku. Through Heroku’s docs and this Get Started with Sinatra on Heroku from 2013, I got it up and running though. The biggest thing was setting up Postgres and what to put in the Procfile. Let me see if I can remember what exactly I did:

  1. Add gem 'pg' to your Gemfile. Also, define your version of ruby, ie. ruby '2.4.1' in your Gemfile.
  2. I changed the sqlite gem to development like this gem 'sqlite3', :group => :development.
  3. I added a config/database.yml because the Heroku docs said to but I don’t know if it’s needed. I’ll probably delete it and see what happens in the near future. The Heroku docs have an example that I used and just changed the database: names.
  4. I think config/environment.rb is where the magic happened. I kept the :development environment using sqlite3 because I read in some of the docs that you needed to know the host, username, password, port and something else to make it work locally. I figured if I could get it to work without figuring that out I would do that. I got this working locally running shotgun and on the Heroku server too. With the changes it now looks like this:
    require 'bundler/setup'
    configure :development do
      ENV['SINATRA_ENV'] ||= "development"
        :adapter => "sqlite3",
        :database => "db/neighborhood#{ENV['SINATRA_ENV']}.sqlite"
    configure :production do
      db = URI.parse(ENV['DATABASE_URL'] || 'postgres://localhost/mydb')
        :adapter => db.scheme == 'postgres' ? 'postgresql' : db.scheme,
        :host     =>,
        :username => db.user,
        :password => db.password,
        :database => db.path[1..-1],
        :encoding => 'utf8'
    require_all 'app'
  5. The Procfile was something I couldn’t figure out. The SitePoint article I linked above had this line for it and this is what made it all work web: bundle exec rackup -p $PORT.

  6. For the rest I followed the Ruby docs on Heroku here and the SQLite doc here.
  7. If you get db migration errors you can run rake tasks on Heroku. heroku run rake db:migrate was one that I needed to do. More in depth docs about it here.

Hey, look at that. I remembered way more than I thought. Hopefully, this was helpful to someone somewhere. Probably a student coming behind me who is going to pull their hair out trying to get their app to work on Heroku.

Time spent today: 8:32
Time spent total: 198:13
Lessons completed today: 1
Lessons completed total: 406

Are you Gifted?

Not much to report today. I got some free passes from being a member of American Mensa to see Gifted. So the wife and I went to see that tonight. It’s a good movie and I recommend it.

I finished up the controller and views for one part of my Sinatra project. It’s coming along nicely. Now that I have this done I don’t think the next two controllers and associated views will take as long. It should be pretty close to the same thing just modifying some small amounts of code. I am still deciding between radio buttons and a dropdown for some of my create and update forms. Parents will only be allowed to be associated with one house so deciding how to visually represent that. I think the dropdown will work better only because this app could potentially have a lot of entries in it. I also need to code in that when a House is deleted so are the members of the house. However, I’ll work on that once I actually have associated members.

Time spent today: 1:00
Time spent total: 189:40
Lessons completed today: 0
Lessons completed total: 405