Strong Params Can Change

Not much going on but completing a couple larger labs tonight. One significant thing that halted me for a while was an issue in my strong params. I had:

def song_params
  params.permit(:title, :released, :release_year, :artist_name, :genre)
end

Which looks really nice but doesn’t take into account that I’m using form_for and thus my submitted data is actually a nested hash. A quick change to:

def song_params
  params.require(:song).permit(:title, :released, :release_year, :artist_name, :genre)
end

Had me moving along again. That’s all I have the energy to type tonight since my 4-month-old had me up at 6am this morning.

Time spent today: 2:38
Time spent total: 216:21
Lessons completed today: 2
Lessons completed total: 445

The Speaker-Listener Technique

I can happily say that I got zero, yes 0 coding done today. Well, I guess I got a little in during my 45 minutes Sinatra Project review this morning. Which was a great review btw.

Hi Seth,

Awesome job on your Sinatra Project!! You did a great job, and I really appreciate your work on the Active Record relationships and your clean and concise code. As you move forward these practices will help you a lot.

Please reach out if you have any questions or concerns as you move through Rails.

Regards,

A couple things that I shored up/added this morning were Active Record validations for uniqueness on username and email when someone signs up as well as displaying error messages on the sign-up page.

Why am I happy about this? Well on Sunday night my wife and I attended a brief marriage seminar. Just something to provide us with tools to communicate better (something most marriages probably need). Tonight we utilized the take home (a fridge magnet) and had a good conversation. So what does this fridge magnet say? It’s the “Speaker-Listener Technique”.

The Floor
Speaker-Listener Technique

Rules for the Speaker:
1. Speak for yourself. Don’t mind read!
2. Keep statements brief. Don’t go on and on.
3. Stop to let the Listener paraphrase.

Rules for the Listener:
1. Paraphrase what you hear.
2. Focus on the Speaker’s message. Don’t rebut.

Rules for Both:
1. The Speaker has the floor.
2. Speaker keeps the floor while Listener paraphrases.
3. Share the floor.

Needless to say, this concept worked very well tonight for my wife and I. It’s designed for times of conflict but we just used it for a normal conversation. Because of this, I’m happy I missed out on coding. While I miss it I know what’s truly important during this short life and a few hours of Rails is not #1.

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

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
private

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

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
continue.
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 -->
<div>
  <a href="/posts/1">My Title</a>
</div>

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 post.id.

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