Steven Brooks' Blog

Student @Flatiron School

Controller Scope

Well I had two interviews today…and on the second interview I had a questions to which i did not get the full answer to.

The question was, “why should we abstract methods out of the controller and put them into the model in rails?” Actually i think the question was worded a little different but that’s pretty close.

I answered by claiming that you want to abstract methods into the model because a messy controller would be difficult for another programmer to read your code. Now that isn’t wrong, but it isn’t right either.

The full correct answer has to do with scope, and after some help from the friendly interviewer I was able to get the answer. If you create a method in the controller it’s scope is just to that specific controller. If you make a method in the model not only does its controller and view had access to those methods but so do all the other models.

The scope I was introduced to in the past 12 weeks was variable scope, etc. So this was a fun problem to work through.

You learn something new everyday…or at least you should try.

Graduated

Last Friday, myself and 31 other students graduated from The Flatiron School. It was a fantastic 12 weeks and an excellent start to my programming career. We also Karaoke’d a lot Friday night and I still don’t have much of a voice so that probably won’t help all my interviews this week.

Either way I wanted to make a quick post today about some of my goals for the near future.

I feel strong with ruby, I feel I can make anything in ruby, but I also feel weak specifically in meta programming ruby. So on my current to-do list, meta programming in ruby is for certain up there at the top.

Although I spent a good amount of time blogging about and working on my Workout App, I have decided to put that on the back burner for a while. One of the things I got out of working on this application was the javascript and jQuery work. Those are also very high on my list. Last week I completed the first section of the Code Avenger’s javascript tutorial. I have to say this style of tutorial blows all other tutorials out of the water. Because I know ruby I really needed a javascript tutorial which would be a stickler on syntax. Avengers is very good at that and I just wish they would create third level for javascript which is supposed to focus on objects, GUI’s, and events.

Another thing that is high on my list is blogging. As I get heavy into the interviewing process there will be questions I am asked to which I do not know the answer to. I would like to blog about specific questions that I do not know the answer to.

Making Responsive Sites for Mobile

I learned great lessons today about responsive websites. The first lesson it to use a site such as The Responsinator in order to be able to see your application across multiple platforms. Second, use Twitter Bootstrap. Bootstrap comes with a responsive CSS stylesheet so that as long as you don’t set a height/width for certain elements, your work will just become responsive on its own. The third part is that on some mobile devices, such as the iPhone, the device will have auto zoom-out going for some reason. In order to combat this you need to add the line

responsive

to your application and specifically to your layout file if you are using rails. So rather than your site looking nice but having the iPhone zoom out like crazy, it will look something like this

my responsive form

So while Bootstrap is amazing and allows people like me to create responsive sites without actually making them responsive, just remember to add the snippit above to your layout file.

Have to Do Things Yourself

I learned a valuable lesson this morning that I should have learned a while ago actually. Or at least I should have picked up on this earlier.

Last night I was watching a few Railscasts and stumble upon a few interesting CMS videos. The one that I thought was the coolest was Active Admin. Active Admin it an awesome, or so I though, CMS for a rails app.

Although what I said above is a little all over the place the point I eventually learned was that if you really need something in your app, you need to build it yourself. Look at the home page for Active Admin:

active admin

C’mon, this looks amazing. It apparently let’s you filter by anything, have scope, etc., etc. But under the cover, this gem messes with the MVC model and makes it difficult to work with your previous controllers if you are adding Active Admin on top of your app.

This morning I was telling a few classmates before the session started about Active Admin and how cool I thought it was. Our head instructor, Avi Flombaum, shook his head and told me that it really wasn’t that great. It made think of my trading days before I became a student at The Flatiron School. At first I had my charts with a million different indicators on them, and as it turned out, I really didn’t even use any of them. So over time I took off indicators and as I took them off my gains increased.

The truth is, when I was trading, I needed a little as possible. I needed exactly the minimum for me to get the job done, so to speak. Same goes for coding. Yes this Active Admin looks amazing, but the truth is I don’t need fireworks and amazing colors and anything above exactly what I need. I have total confidence that I can make my own CMS pretty easily, and maybe make it pretty with some simple Bootstrap.

The Community Is Not Even Real

Quick post today. Currently having a nice mini lecture on client/server stuff by Blake Johnson.

Anyway real quick I just wanted to get down on paper, or on the keys, how awesome the programming community is.

Yesterday at the end of the day I was struggling with some Google Graph stuff. I wanted to head home a little early to get to the gym so I though I’d throw some darts online and see if any of them stuck.

I posted two questions online hoping that maybe by the time I got home (1.5 hour commute) maybe somebody would help me. I put a question on Stack Overflow and also the Google Chart’s Forum.

I sent my two questions, packed up my keys, and as I opened the door to leave The Flatiron School I get an email response from someone from the Charts forum. I couldn’t believe it. The thread that I asked a question on hadn’t been active at all for a month, and a member responded to me in less than 2 minutes.

Amazing. I have learned so much in 10 weeks and I contribute a the large majority of that to the resources I have to my disposal here at The Flatiron School, but I feel more confident now that even when I am not a student here and on my own at 2am, I will have no trouble getting the help I need. The community is unreal.

Here is the ongoing conversation I am having with one of the users: here.

I am officially not only searching google blogs or forums from now on, but I will not be bashful at all in asking questions to the public community. This is similar to my early resistance to asking questions in class, but the first time I asked a dumb question that was not met with laughter I was over that. Same thing here.

And as a side not for only searching blogs on google now. Yesterday I was having trouble with a dashboard for Google Graph’s and standard google searching wasn’t giving my anything helpful. But then I searched strictly blogs, and not only was the first blog exactly what I was looking for but it was also a hold-your-hand style tutorial that was beyond perfect for what I was looking for. Boom.

In my programming career I have asked two questions to the public forum with both being answered within minutes as well as both responders had back and forth conversations with me. Amazing.

Maybe in my spare time I’ll find a way to do analytics on this. Would be really interesting to see the average time it takes someone to respond to a S-Overflow or forum question. I totally love data crunching so maybe that will be something I can do.

Also I’m thinking of asking this guy what his GitHub is and tip him on GitTip because I would totally do that.

Loving Ruby

It’s been almost a week since I blogged. Before I set out to blog each day I worked on my workout application. I haven’t followed through on that. I’ll tell you the reason I haven’t blogger yet is because every waking minute I am either working on the app itself or trying to finalize our group project for the Science Fair this Thursday.

Anywho, I have done quite a bit of work on my application since last I blogged. Last time we spoke, I was trying to implement three javascript functions into my application; calendar, autocomplete, and the ability to create as many exercises and sets as a user wants. All are done.

I also added bodyweight to a routine and not a user. I talked to myself about this for roughly an hour and as far as a user is concerned, I realized that a user would be updating their weight during each workout rather than going to a user profile page and updating their weight there. To me, it made more sense. People go to the gym, and weight in, so they would be able to input that data when they are inputting the data for their routine.

For the past few days I have been working with Google Graph to be able to display the users stats graphically. I have been living this process. What you have to do is to take your data and manipulate it in order to feed it to Google a certain way. That’s freaking awesome. I totally love making five methods for each array Google needs and then doing string manipulation. In order for me to get what Google wants I have to deliver something like this:

google

Awesome. That’s a challenge I think I can tackle. Especially since my wheelhouse are Ruby methods, I knew I was bona hit this shit to Beijing. In this information I need to set the first array to be the titles of what I want:

google

Here is that code running in the rails console. You give it an instance of the routine class and it gives you the right amount of information. Here is will write “sets…” as many times as the most sets per the lifts within the instance of the routine:

console

Next time we chat it up I’ll show you more stuff.

2/3 Jquery

So I’m 2/3 of the way there with my javascript/jquery wants. Yesterday I mentioned that I wanted to have a calendar to properly keep track of workout days rather than active record’s created_at, I wanted to autocomplete the exercise name so that in the controller/model I can find_or_create_by for an exercise rather than a drop down, and I wanted to have a form that started with one exercise and a user can add as many sets to that as they choose as well as more exercises with sets.

As I’m sure you’ve assumed by now I completed the first two but not the last. Here is functioning code for the calendar:

My params

Here is the code I posted yesterday which was an attempt at autocomplete which didn’t work…can you spot the errors?

My params

From what I understand, or at least from the changes I made along with a JS Ninja (Jon Grover), I had a few semicolon issues as well as placing the array after the function. I don’t know for sure but it felt like I had to create the array for the autocomplete before that array is called in the function. Here is the javascript code that allows both the calendar and the autocomplete to function properly:

My params

The issue I have been having on creating a new exercise is that javascript, or at least my javascript, does like the idea of appending and ERB tag. I’ll get to the bottom of it though. I’d like to save the before and after code for the same post as I feel it would be super useful for a lot of people as it’s own post. Cool beans.

Also, over the weekend I made a ton of methods to find out certain details about my workouts, such as best sets, most weight used, % increase in total weight over time, etc. Really pumped about those and after this form is finished up I am going to move on to different layouts so that I can graphically show the cool data I came up with. One thing at a time though.

Day IDK…Getting Busy With JQuery

It’s been a few days since I have blogged and consequently a few days since I have been able to work on my Workout App. That will change this week.

Over the weekend when I was able to work on my app I was split between the next step to take. I had two options: make the form better by adding Jquery to it or start making methods that will be used when a user wants to do some form of analytics on their workouts. Although to date I prefer the backend and making methods, I decided to take a shot at the Jquery stuff, which I am a total newbie in.

My first stop was visiting the Jquery UI website. I totally love this site. This site makes it so easy for me to do the programmers version of drop and drag for many awesome front end features. Looking through what they have available, I realized I would like an autocomplete function for the user to choose their exercises that way as apposed to a select list that could get very long. Also, an autocomplete function would allow me to find_or_create_by an exercise! Next I would like to have a calendar for the user to say when that workout will or had been completed. Although active record gives a created_at and updated_at attribute for everything in any table, a user may do a weeks worth of workouts and create the routines and information all in one day. Active record might consider those created_at very similar times, so when a user goes to look back at their workouts overt time, I want that information to be accurate. I also would love to have a form that lists one exercise at a time and a user chooses how many sets per each exercise, and after they complete the information for one exercise and it’s sets they create another exercise and so on.

Out of the three I was able to complete just the calendar. I do not know why the autocomplete is not working and I am getting a few nice errors with my last query function.

Here is the current code to implement that calendar:

My params

Simple enough.

Here is my current code for the autocomplete:

My params

The other code for creating new form fields on the fly is a mess. Either way I will show the correct code for all of the three things I would like to do in the following days.

Day 7…Took Four Models Into One Model

After over a week of tinkering with my form for my workout app, spending hours with TA’s, and even looking to the Stack Overflow community, I have finally figured out my form. I now know how to create a form where four models are being used, create all of their associations in the controller, and display them properly in the view. That’s Rails ladies and gentlemen.

Here is my form (which will be changed with some jquery soon):

My params

The trick here was that I wanted many instances of “lifts” in an array, so creating the variable “n” and interpolating that variable into the form fields basically set “n” as a counter.

Then this is how I tied everything together in the controller (which will probably be cleaned up as some point):

My params

Not bad at all.

Day 6…Hey Dude Remember There Are Keys and Values in Hashes

Got it. Finally. I couldn’t be happier right now.

I’m going to reflect a little here. I’ve been programming for 8 weeks now. In that time, I have learned Ruby, SQL, Git, Sintra, and now Rails. Holy shit.

While those are remarkable I now truly consider myself a programmer. In my latest blog post, I mentioned that I was going down a nice long rabbit hole. But I’m here, and you know what, I’m totally out of that rabbit hole now. I am now able to know exactly where some of my errors are and also where I can go to get those errors solved. On my StackOverflow question, I was able to articulate my question well enough so that a true Rubyist would be able to answer my questions and for me to understand what he was saying.

Here is my form that will scale and will work. I whooped you ass.

My form