Transcripts

Coding 101 66 (Transcript)

(Net casts you love from people you trust. This is Twit! Bandwidth for Coding 101 is provided by Cachefly at cachefly.com.

This episode of Coding 101 is brought to you by Linda.com, the online learning platform with over 3,000 on demand video courses to help you strengthen your business, technology and creative skills. For a free 10 day trial visit Lynda.com/c101, that’s Lynda.com/101.

And by Digital Ocean; simple and fast Cloud hosting built for developers. Deploy an SSD could server in 55 seconds. Try it today for free. Visit digitalocean.com and once you sign up be sure to enter the promo code c101 in the billing section for a $10 credit.)

Father Robert Ballecer: On this episode of Coding 101 it’s time to jump into Rails. Welcome to Coding 101. It’s where the power of code compels you. I’m Father Robert Ballecer.

Lou Maresca: And I’m Lou Maresca.

Fr. Robert: And for the next 45 minutes to an hour or so we’re going to let you into the secrets of the Code Monkey and the Code Warrior. Now Lou last week we had a chance to speak with Carlos Souza and he gave us a fantastic introduction to Ruby but you know this; Ruby by itself is interesting but until you put it into a frame work you don’t really have a powerful language right, which is why this week we’re going to go into Rails. Have you played with Rails at all?

Lou: Yes I have. It’s actually a pretty easy going framework to get ahold of and tool set to get ahold of and again it gets you really quick up and running and so I’ve played with it a little bit to kind of get going and to understand where everything is going.

Fr. Robert: That’s been my experience with Ruby and with Rails which is it’s an easy language to learn and then when you start doing all the hooks you start going “oh wow; why doesn’t every language do it like this?” Why isn’t every framework like this? Now we’re going to get into that but as is our custom before we do that we’ve got to cover a little bit about the news. Now Lou, you’ve got a little something here about coder to programmer. Now we’ve talked about this paradox before. We’re not trying to put anyone down but there’s clearly a difference between people who’ve just learned to regurgitate code or maybe offer up a couple of insightful uses of different logic trees and then the programmer. The programmer is the person who has that skill, desire and passion to keep finding out new ways to solve a problem. What does that mean exactly though?

Lou: This was a really great article and I thought I’d bring it up. It’s from the Fire Hose project which is actually a Ruby project. But what they’re doing is they’re trying to teach people how to become programmers. Not just become coders. Coders are just people who learn how to do the language, they need a lot of tutorials, and they need to kind of have a lot of people walk them through stuff. This whole idea is how do they become… they talk about what they call the inflection point and that’s the point where you realize, oh I’m now a programmer.

Fr. Robert: Actually I could see that because it’s very easy when you’re just doing a boot camp. Because let’s be honest boot camps are kind of the rage. That’s how you learn rather than sitting down or going to a traditional schooling method like a university or a college or even a trade school. You want to get 8 weeks of just intense immersive programming. That’s the thing. But even in those programs there has to be at some point where you realize that you’ve got enough of the knowledge, you’ve got a critical mass of knowledge about a particular language where you realize oh I can start taking this off on my own. I no longer have to follow the examples that are given to me. I can figure out another way to do xyz.

Lou: They kind of break it up in 2 points. They say there’s what they call a tutorial phase which they say is about 3-8 weeks and they talk about how you start to recognize patterns at that point but they’re still kind of walking you through things. But then there’s a 2nd point which is called the inflection point and that’s the point where you all of a sudden just realize there is a bigger world out there – let me start moving beyond that and doing my own thing. There’s something very similar to that, there’s a guy by the name of Dave Thomas and you know him as the Wendy’s Guy…he doesn’t make burgers. But this guy he actually wrote a book called the Pragmatic Programmer and he also does a lot of talks externally. He talks about this model called the Dreyfuss model of understanding and that model has I think 5 different layers if I remember correctly and it talks about how you start out as a novice and you still need people to tell you want to do. You really can only do 30 minutes worth of work and then you start to move into what they call the advanced beginner mode and that mode is where you start to kind of really understand things but you still need kind of hand-holding. But then you get into the point, level 3 where you’re confident. Now you’ve realized there’s a bigger world out there. It’s kind of like a 2 year old when they begin to walk. They start running around the house thinking they own the world but then it’s to the point where they get to the front door and they open the front door and they realize wow; this world is a lot bigger than I experienced. That’s the level 4 – the proficient. At that point you almost feel like you take a step backwards because you realize wow this is a much bigger world that I’ve experienced, I need to learn a lot more, and mentally take that step out.

Fr. Robert: That’s natural to learning and that’s what happens in any programming language where you realize you’re not just looking for that solution that you have stored in your memory of oh I know this pattern, this is the solution. But you realize here’s a pattern, I wonder how I could solve this. I think you’re absolutely right, that’s that differentiation between the coder and the programmer. The coder is just going to keep regurgitating the same sad solution whereas the programmer is going to say there might be a more efficient way to do this. Let’s talk a little bit about that pattern recognition because that’s not the 1st time I’ve heard about it and we actually haven’t talked about it much on Coding 101. It’s something that every programmer and coder needs to have which is, when I look through the logic tree of what I want to have and what my inputs are, what are my goals, my outputs and what happens in between I need to start recognizing certain patterns. And those patterns will determine what algorithmic solutions I use. When you’re programming what are the patterns that you most often see?

Lou: It’s all dependent on the data that you’re using. So like for instance the pattern that I like following for instance if I’m throwing lots of customer data around maybe understanding their geo location or something like that, I need to understand the data before I start to understand the pattern. So that’s a bit one to follow. Another thing is understanding the system that you’re using because a lot of software out there… there’s 3rd party software, there’s systems and services that you use so you need to understand how those services are being used so that you can understand how you can regurgitate that service and be able to understand how to use it. So those are a few things I like to follow.

Fr. Robert: Now speaking of patterns we had a chance to once again speak with our Code Warrior Mr. Carlos Souza and he’s taking us into the Rails framework now which is perfect because in order to use a framework properly you really do need to recognize those patterns. We’ll be jumping over to Carlos Souza in just a second but before we do that let’s go ahead and thank the 1st sponsor of this episode of Coding 101.

Of course the 1st sponsor of this episode of Coding 101 has to be Lynda. Now what is Lynda.com? Lynda.com is the online repository for knowledge. For all knowledge. If you are a developer or someone trying to learn new business skills or maybe you just want to pick up a hobby Lynda.com is the place to go online when you need to pour knowledge into your knowledge hole. Now Lynda.com for us has been a place for problem solvers, for the curious, for people who want to make things happen. Maybe you want to talk better photos, design a responsive website, master Photo Shop or sharpen your Excel skills. Lynda.com has everything you need to feed your curious mind. Lynda.com is a series of courses that I recommend in particular; it’s called “The Foundations of Programming”. We talked about this on Coding 101, if you don’t have the foundations, if you don’t understand what’s actually going on then what are you doing? Now there are installments covering fundamentals, working with data bases, data structures, design patterns, code efficiency and more. There’s even a fundamentals on programming for kids. Regardless of your experience level you’re going to want to check out the series. Lynda.com also has the essential training series which covers different programming languages like PHP, and J query as well as developing for specific platforms like Android and iOS. Now we here at the brick house have been using Lynda.com for our transition from Apple with Final Cut Pro to Windows PC with Adobe Premier Pro. Lynda.com is perfect for that kind of knowledge because they’ve got searchable transcripts, they’ve got excellent videos. Because you can go straight to the answer rather than messing around with video after video after video. Now with Lynda.com you can watch and learn from top experts who are passionate about teaching and they’re the top in their fields. You can search thousands of video courses on demand and most importantly learn on your own schedule. That means that you learn at your own pace, at your own desire. Courses are structured so that you can watch them from start to finish or consume them in bite size pieces. Now you can take notes as you go and refer to them later and you can download tutorials and watch them on the go; including access on your iOS or Android device. They let you create and save play lists of courses that you want to watch so you can queue up your learning. Lynda.com really has something for everyone. Speaking of something for everyone your Lynda.com membership gives you unlimited access to training on hundreds of topics all for 1 flat rate. Whether you’re looking to become an expert, you’re passionate about a hobby or you just want to learn something new (I know you do because you’re watching Coding 101) I want you to visit Lynda.com/c101 and sign up for your free 10 day trial. That’s Lynda.com/c101 and we thank Lynda.com for their support of Coding 101.

Thank you Lou and Padre. I’m here on the sky desk and I got a chance to speak with our Ruby and Rails guru Carlos Souza from Code School. Carlos thank you very much for coming back.

Carlos Souza: Thank you very much for having me Padre. It’s a pleasure to be here.

Fr. Robert: Carlos last week you dropped the knowledge on the Coding 101 army about Ruby and you showed us why it is a language that is more concerned with how programmers program rather than with how computers take that code and turn it into something that they can understand. Now we saw some very interesting features from Ruby but now we’re moving beyond Ruby. You want us to move into Rails. Why is that? Rails is a framework but why can’t we just stay with Ruby?

Carlos: Rails is a web framework that takes Ruby to a whole other level. It’s built on top of Ruby and the same productivity that it gives Ruby programmers it gives web programmers. So we can program now for the web with the huge productivity.

Fr. Robert: I got it. So of course Ruby is a language. Ruby is a very interesting language but Rails allows us to take that and push it server side.

Carlos: Exactly. It allows us to build full stack web applications.

Fr. Robert: Alright. If I’m a complete newbie here and I just started getting my feet wet in Ruby and now I’m moving into Rails where do I start?

Carlos: The first place to start would be the Rails website itself. So RubyonRails.org is an excellent website that you can go and check out a bunch of resources and there’s links to screencast presentations, book and a bunch of other stuff and then towards the end of the episode we’re going to talk about a couple of different places where you can go to try out Rails in the browser. So that is my recommendation to try and see what Rails is all about.

Fr. Robert: What kind of complexity are they looking at here? Let’s say that they followed your homework from last week and they have a decent working knowledge of how Ruby works. How much more complicated is it going to get to install Rails and get that up and running?

Carlos: Installing Rails really depends on what type of machine you’re using. Windows tends to be a little bit more complicated than on Mac and Linux machines but it is not super hard.

Fr. Robert: Alright. Now once they’ve got that installed and we may have to do a knowhow on how to get that up and running. Just because I know there are going to be some who are going to want it step by step. What’s the 1st part of programming that they’re going to want to get into?

Carlos: The 1st part of programming in Rails is I want to say just start your Rails application. So this is what’s cool about Rails, it give you a bunch of commands to create stuff for you. Because Rails tries to combine, it tries to group all of the best practices in conventions that are used across the majority of web applications. So it gets your outdoor giving your 80% of what all the web applications need. So all you really have to worry about is the 20% if that makes sense.

Fr. Robert: Yes it does. Ok so I’ve started it up and if I want to create my 1st application how would I do this?

Carlos: Let’s do it right now.

Fr. Robert: Alright.

Carlos: Ok so I’m here on my terminal and I have Ruby installed and I’m using version 2.2.1 which is the most recent version as of this recording. I already have Rails installed and I’m using Rails 4.2.1 which is also the most recent version as of this recording. Now I don’t have any files at all and I’m going to tell Rails that I’m going to create a new application. So let me clear this up. Alright cool. So Rails has a command line which is the Rails command and it gives us a couple of hints of where to proceed. So if you just type Rails new it’s going to say hey you need an argument for new. So the argument is going to be the name of the project which is also going to be the name of the folder that it will create for us. So let’s create a project called Reading List. When we do Rails new reading list it’s going to create a bunch of files, a bunch of folders inside of that new reading list folder that it just created. So now if we do a list of the list of files we can see that there’s a reading list folder. Let’s jump into that folder and… let me make this a little bit smaller…and you can see there’s all these files that Rails automatically created for us. Before we jump into them I’m going to go ahead and just run our Rails server. So each Rails application comes with its own standalone Rails server. That’s what I’m going to run now and to do that we run Rails server or just a short cut Rails s.  So we run that. Our server started and it’s saying it’s listing a local host port 3000. So if we go now to local host 300 we can kind of see that the full Rails welcome message. What’s cool about this is if you’ve ever worked with any other web framework or pretty much any other program language you know that each step you take you want to make sure that you’re going in the right direction. So when you load local host on your brand new Rails application and you see a page like this, this is a success message that you were expecting right so everything is working. Rails was properly installed which you know and your application was properly created. So it not only give you the success message saying Welcome Aboard, you’re riding Ruby on Rails but it also gives you a couple of hints on where to go next.

Fr. Robert: What would be some examples of that process not going well? The dependencies don’t get set properly, it doesn’t start up a new server for you to use; is it as long as I get that message everything is good or are there certain other things that I need to make sure are there before I step into the next procedure?

Carlos: So for this first step if you see this phase you’re good. Some of the errors that you might encounter are maybe you’re running another service on port 3000 and then when you try to load it you see a different page. And when you see a different page you might be surprised; well I thought I had my Rails server running but perhaps you have another Rails application or a different application altogether running on that port. So you might see a different message. So this is kind of like that safe point where you see this, you know that your application was created successfully and everything is running at this point.

Fr. Robert: So by and far for our audience who will probably not be running any other Rails installations, who will probably not have any other services running on the machine that they’re going to be programming on, as long as they get to that screen they’re good to go.

Carlos: Exactly. That screen is good to go.

Fr. Robert: Step us through.

Carlos: Let’s go and create our 1st little custom page and Rails is an MVC framework so it follows the MVC pattern where we have our models, our views and controllers and these different aspects of the application usually are concerned with different parts. So that’s the typical way that we split our concerns in our application. Something either goes into a model, a controller or a view. So let’s start with a routes file. In a routes file we want to tell it that we want to go say to Welcome. If you want to zoom in here it says /welcome and that’s the URL that we want to add to our application. Let’s go ahead and open our routes file which again was created by Rails for us. We did not create this file. So if we open it up we can see there’s stuff already written in there and it’s again another really cool aspect of Rails. It’s pretty good documented. As you can see here this is actually the documentation on how to use the routes file. It’s telling you how the routes work, how you can write your routes and different examples of code that you can write in there.

Fr. Robert: Very nice.

Carlos: It’s always a good reminder. I’m going to go ahead and create a root route. A root route means that every time that we go to slash we want to go to this specific controller and action. So the route rout is going to be; welcome, index. Which is the same thing that it says here in the documentation. So just for example purposes. Now if we go to the application again… oh our server isn’t running. Let’s close this and run our server. There we go. Now we see this error message and it says uninitialized constant Welcome Controller. I’d like to say this is a good type of error because it tells us what to do next basically. It’s saying hey we couldn’t find the welcome controller so the next step is to create this welcome controller which we haven’t yet. So I’m going to leave the server running in this tab and I’m going to open another tab and create our welcome controller and it’s just a class that inherits from application controller. Now if we save this. This is what inheritance looks like in Ruby. We’re creating a class that inherits from another class and this is a class that’s a Rails class basically. If we go back and run our page again. Now it’s giving us a different error. It’s saying that we couldn’t find an index action for the welcome controller. The action is nothing but an instance method which is very similar to the one we created on our previous episode.

Fr. Robert: Now application controller, that’s a default right? What’s contained in there?

Carlos: This is application controller. It already exists in Rails. So it creates it for us, it’s there. So all of our controllers are going to inherit from application controller. As you notice here application controller inherits from another controller called action controller base. This is pretty much the pattern on most object oriented web frameworks or pretty much any object oriented framework. A framework is a tool box so it basically gives you a bunch of classes that inherit from other classes. When you want to creator application you create your classes…

Fr. Robert: And you inherit some other classes.

Carlos: Exactly. They inherit some classes from the framework. Alright so index controller; what I’m going to do here is I’m going to render a text straight from the controller. So what I’m going to do is render…called the render method, text end, and options hash saying O hai. When we run this again you can see O hai is printed to the screen.

Fr. Robert: Can I define other destinations within that controller?

Carlos: Yes you can. When we start doing something, a couple things more complex where we’re going to see different actions, different routes. The index action is typically used for listing stuff. We’re going to look at different methods, actions to create stuff to delete stuff to update stuff. We can see we’ve rendered a text on the screen. Pretty simple, pretty straightforward. This is just fling text. If you open up the source you can see this is just fling text. But if we want to return some HTML, what if we wanted this to be an h1. It works, it’s an h1 but it’s not really good if we start writing our HTML inside of our actions like this.

Fr. Robert: Yes, that would get a little unwieldy.

Carlos: That would get kind of weird. This is a controller. Now we’re going to move onto the view. So let’s move on and create a view file. If we look at the folders that Rails creates for us it already created a folder for view as you can see here – the last one. All of these folders were created by Rails. We didn’t create anything. Inside of our views we’re going to create a folder that’s named after our controller. This is so that we can leverage something that Rails calls convention over configuration. So if you follow the Rails conventions there’s very little stuff that you have to configure in your app. So I’m not going to say where to look for the welcome view, for the index view from the welcome controller. By following the convention of naming the folder welcome which is the same name as the controller Rails knows how to look for each convention named after that file. I’m going to create an index file and I’m going to use the ERB templating language. This is just a folder and now I’m going to create the index.html.erb. Then inside of here I’m just going to write HTML.

Fr. Robert: That makes much more sense. That doesn’t need to be in the controller.

Carlos: Exactly. So I wrote my HTML and just to make sure that my controller now doesn’t have any of this and if I render it now you can see it still prints O hai. Just so you know… h2 from template. So this is the template. As you can see we didn’t have to write the code that said to look for this specific view when we hit that URL. This is all a convention. You can even take this 1 step further because we’re not doing anything in here so we can pretty much kill this and Rails still knows that it needs to render the index view.

Fr. Robert: Oh, is it just because it is a default because if there’s nothing in the controller…

Carlos: Yes. It’s the convention.

Fr. Robert: Alright.

Carlos: That’s the 1st step. Now let’s say that we want to add some interactivity with the user. It’s going to be very basic stuff. Let’s add some links here. Let’s go ahead and do a link to a search engine. If you’re familiar with HTML you can probably figure out what this is going to render. It’s going to render a link which when you click it takes us to Google right. This is the pure HTML. We can use Rails helpers to write this for us in a much shorter syntax. So we can say link to and http dot com. I think search in 1st. Yes like that.

Fr. Robert: And again we’d just be using this for consistency.

Carlos: Yes for consistency and also because it’s more of a Rails style as well. You don’t have to remember the html syntax. Although this is a very simple example rails has helpers for other stuff as well. So if you want to send out, if you want to link that to an email it knows what to do and… ok let’s stick with the Rails one for now. Search, google and you can see the link down at the bottom. It’s going to open up Google.

Fr. Robert: Alright, that gives us our controller that gives us our view.

Carlos: Right. There’s no interactivity yet. So what we want to do here is add a query string parameter. We want to be able to set which search engine we want to use. So suppose we want to use Yahoo instead of Goggle, let’s say that we want to pass this as a URL parameter here at the top. So all we’re doing is saying search equals Yahoo and we want this to point to Yahoo.com instead of Google.com.

Fr. Robert: Zach why don’t you go ahead and zoom in on this a little bit just so our audience isn’t squinting so much.

Carlos: Yes, so we want to be able to pass a parameter. In this case Yahoo or we could use Bing and we want this to point to Bing instead. So let’s do that. Now we have to define our index action because we’re going to be setting an instance variable here. Again this is where the object oriented principles come into place. Anything we set here, any instance variable that we set inside of this action can be accessed from our view using that string interpolation technique that we learned. So if you remember from our previous Ruby example this is pulling out a variable and evaluating and replacing it. Right now it’s just hard-coded to Google. But it should work. So if we go here… it’s not supposed to be Bing. Here you can see at the bottom is Google.com. Click it and it goes to Google.

Fr. Robert: Of course now we need a way to make that not a hard-coded variable.

Carlos: Exactly so we need to read from the query string parameters so let’s go ahead and do that. Here we’re going to read from our params and this is an object that we get from Rails and let’s call this search. There’s our symbol again. Anything you put out there it’s going to replace so now if I click here it takes us to Bing.

Fr. Robert: How many parameters can I pull in?

Carlos: As many as you want. If you do Yahoo it takes you to Yahoo but if you leave this out it is going to error out. It doesn’t have a value for that.

Fr. Robert: Right because you didn’t fill the variable with anything.

Carlos: Exactly.

Fr. Robert: I can set a default parameter can’t I?

Carlos: Exactly. Let’s see how we can do that. There’s a couple different ways. I’m going to start with the more verbose and then shorten it up. Just like we did before. We could do something like this. Let’s do if there’s a param search we’re going to set search, param search, else we’re going to have search equal to Google.com, end. Close this. In Ruby anything that’s not null or false validates to true. So if there’s a value set to search this is going to validate to true and then we use that to set the search.

Fr. Robert: How good is Ruby on Rails at garbage checking and error correcting? If someone starts typing in some really strange parameters do I have to throw a lot of checking into that to make sure they’re not trying to hit something on my backend?

Carlos: If you follow the Rails conventions you don’t have to worry too much. Because I want to say that the same way that Rails gets you 80% of the way into building web apps it protects you from 80% of stuff that’s out there and that’s typical attacks on web applications.

Fr. Robert: That’s one of the biggest issues with Java which is Java will do basically what you tell it to do and if you tell it to check for that you’ll be in trouble. So Ruby by default if you follow the conventions which you’re handing out now…I’m sorry Rails, it will take care of 80% of that.

Carlos: Yes a lot of stuff will be taken care of for you. As we’re going to see in the next episode when you’re using the active record framework and it already protects you from Sequel injection and a bunch of other things like web attacks.

Fr. Robert: Which is 90% of the script kiddie stuff.

Carlos: Right, exactly.

Fr. Robert: Alright, keep going. So now we’ve got a way to have a default value, we’ve got a way to take parameters and put them into our view. What’s next?

Carlos: Let’s just shorten this code. This is the most straightforward way if you’re coming from other languages.

Fr. Robert: Oh so now you’re going to give us the Ruby way to do it.

Carlos: Yes. So if you say search it’s going to be search. (Let’s just kill everything here) or google like that.

Fr. Robert: I love it.

Carlos: So that’s the short circuit right, the “or”. If this evaluates to false, if it’s empty it’s going to jump to the other side of the pipes when it’s false and it’s just going to set to Google.

Fr. Robert: It seems so strange to me but yes that’s a better way to do it. It’s such a better way to do it. So just to make sure it works if we render this, no parameters, this takes us to Google. Now let’s add a very simple business rule here. Let’s say that people we accessing our site and they were misspelling Yahoo right. So if you want to zoom here to the URL you can see that I’m misspelling Yahoo. Instead of Yahoo I’m saying yahu. Alright, so if we try that obviously it’s going to link to that exact stream and takes us…wow look at that, it redirected us. I didn’t know.

Fr. Robert: Ok that’s not you, that’s Yahoo being clever.

Carlos: Yes that is Yahoo being clever. Let’s do something else stupid. Alight, Yahaaa.

Fr. Robert: Right, they can’t have them all.

Carlos: Yes. Let’s say people are misspelling it right, calling it that. So we wouldn’t want this link to lead to Yahaaa. Let’s say we wanted to correct this to point to Yahoo. For everybody that passed this misspelled version right. So what we could do is something like that. How did I write it, yahaaaa, 2, 3, and 4? That’s 1 way to do it.

Fr. Robert: That would be a very complicated way to do it. To actually come up with all the different ways they might misspell.

Carlos: Right but it solves our issue. So right now it is linking to Yahoo.com. But that is not really the problem that I wanted to point out. The problem I wanted to point out here is the fact that we’re writing this logic inside of the control floor. Like you said if we were to match on all the possibilities of misspelling we’d have to come up with a bunch of different things. But the point here is not that the logic for some of this is stupid which it is because it’s sort of contrived. What I want to point out is that the logic for this does not belong inside of the controller. It belongs inside of the model. That is where the business logic should go. So instead of having this over here the next step would be to extract this business logic like I said to a model. I’m just going to start writing what the model will need to look like and then we’ll create the model itself. Let’s say this would be sort of like search term, new and again remember this is just a Ruby class. This is the constructer and instead of search this would be search term. And search would be search term and I want to call this engine or something like that. We’ll move all this inside of our model. That model we’ll call search term and another convention. We’re not telling Ruby where to look for this model. We’re calling the model here a controller and we’re naming a file in the models folder. So we’re not telling Rails how to find that model, we’re following the convention. I’m going to create a class called search term and inside the class create the constructer.  If you remember here from the controller the constructer takes the param search, so I’m going to call a search and then we’ll do all of our business logic inside here. That example “if search equals Yahaaa then return Yahoo”, else return search. Remember we went over this in the previous episode, we don’t have to write explicit returns.

Fr. Robert: Right, it figures out what you want to return.

Carlos: It figures out that it’s the last statement of… I did something wrong, it’s not initializing. So that was a constructer.

Fr. Robert: Now we’re getting closer to what a MVC should look like which is the view is just the stuff that we see, the controller is just updating the state and the model is actually changing the view when a state has been changed.

Carlos: Exactly. Controller shouldn’t have a lot of code inside of their actions. You might want to keep it around maybe 2 or 4 and perhaps… but the thing is business logic should not go in the controller. That is the rule of thumb. Right now we have our model here that’s doing all the logic and then our controller is just calling the amount, the model and then we can see that everything still works. When we try to access yahaaaa it corrects to Yahoo or if we pass Bing… So it knows Bing or if we default it knows that it’s not… Let’s do this here too. I can actually do it up here too. Search or Google...the code we had over there. So you can see we’re changing the model, we’re not changing the controller. The API is still the same. This is a good example because right now because I don’t want to introduce this yet but this is a perfect scenario for unit tests; to write automated unit tests. If we were to write automated unit tests for this model we wouldn’t have to rely on Rails to run those tests. We write very focused and very fast unit tests that test our business logic for our application.

Fr. Robert: Carlos, we are just about out of time for this particular episode but I’d like for you to pass on some final wisdom for the folks who are wondering about this model view controller model. You said a few times as long as you stick to conventions, stick to the convention that things work better. Ruby and Rails will protect itself and you get everything where it is supposed to be. Could you please explain in no uncertain terms why it is so important that you not put business logic in the controller and why you keep the view to only the things that the user should be able to see? Explain why those pieces need to stay where they are otherwise things break.

Carlos: 1st because it just makes the application easier to read, it makes it easier to understand and it makes it easier to maintain not only by you but by other people working on your team. Because changes are the controller will respond to different actions so every time that you need to edit business logic or 1 specific action, if you need to edit that same controller and then you have other people working on your team, changes are that you’re going to have multiple people working on the same file which increases the chances of conflict if you’re working with something like Gits or Mercurio or some sort of virtual control system. It’s going to cause a bunch of errors and conflicts and it’s also harder to test. So the thing is if you’re able to organize your stuff in components and modules, calls so to speak, if you’re able to focus each concern on different places, on very focused, on very granular components it’s just easier to maintain your application. And it’s easier to have more people in your team working on the same code base without stepping into each other’s toes.

Fr. Robert: Carlos thank you again for being our Ruby and Rails guru. Next time we’re going to be stepping beyond basic input output. I’m very excited. But I understand you’ve got a little bit of homework for the folks at home who maybe want to catch up on what you’ve been doing here.

Carlos: Yes, absolutely. If you’re home and you want to try to take 1 step further than what we did here what I want you to try to do is try to be able to read multiple arguments from the URL and generate multiple links. So suppose that in the URL we want to be able to pass Bing, Yahoo and Google and then the view needs to print “search Bing, search Yahoo and search Google.

Fr. Robert: There you go folks. That’s the challenge, the gauntlet has been thrown. If you do come up with a viable solution make sure to post it into our Google Plus group and we’ll show it off on that episode. Now Carlos I know that you’re at Code School and we learned last week that Code School is a great place to get knowledge about programming. No matter what kind of web programming knowledge you want, what kind of web application you want to build you’re going to find it with the experts at Code School. I treasure that, that’s fantastic but could you please tell the folks at home where they can find you? I know your GitHub is impressive. If they want to go a bit beyond what you’re teaching here that’s a good place to start but where else can they find you on the inter-webs?

Carlos: My Twitter is @caike, it’s down here below my name. That’s what it looks like. People can follow me over there and also on Codeschool.com or like you said my GitHub is the same handle – caike and they can check out all the projects that I contribute to. My personal projects, all the different sort of stuff that I push out there and I think we mentioned Rails for Zombies at some point…

Fr. Robert: Yes. Do it.

Carlos: So if people want to try out Rails but don’t want to spend the time installing it on their machines Rails for Zombies is one of our free courses at Code School. It’s a great way to get your feet wet and see if this Rails thing is for you. It’s all in the browser, you don’t have to install anything and you get to code Rails inside of a browser. You watch a couple of short videos and then after the videos it’ll give you a couple of challenges that you can go through and earn points and get a badge and all that stuff. By the end of the course you should have a pretty good idea of the Rails architecture and some of the conventions that we talked about here. You can figure out whether or not you should invest any more time into your Rails journey.

Fr. Robert: There you have it folks. We give you a little bit of a taste here on Coding 101 but if you want to dive deeper into Rails… and you should because this is an exciting framework – go ahead and try Rails the zombie way. Again Carlos Souza from Code School thank you very much for being our code warrior, our code guru and we will see you next time.

Now let’s take some time to talk about something that is near and dear to every code warrior or code monkey’s heart and that is what do you do with your applications, your services, your hard earned programming once you’ve completed it? The old way was to build up some servers, build up some iron, maybe buy a rack or a server in a rack or a piece of a server in a rack and maybe you can get your service up and running. Of course you had to manage that, you had to make sure it was running and it was your responsibility to update it and do all those things or hire someone to do it. Sure you could go that route and that’s probably the route that I started off with because I like getting my hands dirty but that’s not really the way it works any more. These days you want to be able to take your application, wrap it up and get it running immediately. That’s why we’ve got Digital Ocean as a supporter of Coding 101. Now what is Digital Ocean? If you’re an experienced code warrior or just getting started then you know what Digital Ocean is; it’s a flexible, reliable and affordable hosting option. Digital Ocean provides developers with droplets which are virtual private servers that can be customized and deployed quickly to host websites, web apps, production applications, personal projects, virtual desktops and pretty much anything else that you can think of, all with root access. We’ve been using Digital Ocean here for the last couple of months to play with applications that we think we want to deploy either in house or through the internet. It’s a great way to test our programming and maybe even open it up to members of the public so that they can help by hammering it, to break it, to show us the exploits so that we can make everything right before we go full public. That’s what digital Ocean is for. Everything from sandbox to full production. Digital Ocean is built by developers for developers and it’s used by over 400,000 of them including me. You can deploy and configure your droplets via a streamlined control panel or simple API. You can choose your OS, Ubuntu, set OS, Fedora and Freebee SD, 1 click install allows you to quickly deploy apps like Jango, Docker, Droople, Git lab, media-wiki, Word Press, Ruby on Rails and more. Now all servers are built on hex core machines with dedicated ECC RAM and Raid SSD storage. Servers can have up to 20 CPUs, 64 gigabytes of memory and 640 gigabytes of SSD space. They are highly scalable to meet the demands of rapidly growing application or business and auto backups and snap shots that you can easily clone, deploy and resize your droplets as you grow. Now my personal favorite is the fact that these are going to be deployed in regions across with world with gigabit speeds; that means 99.99% out time. You get full featured DNS management and easy to manage domains or if you want you could use dedicated IPs. They give you full console access with HTML5 plus SSH SFTP and KVM/VMC for virtual desktops. They’ve got an extremely active community with a large and detailed set of tutorials so that you can use your droplets with the support of your community. If you want to deploy Docker well go ahead, if you want to set up a personal VPN you can do that. If you just want to run yourself a web server well Digital Ocean has got you covered there too. Really it’s so easy to just get started. You can just deploy your own Cloud in literally minutes. Now here’s what we want you to do. Digital Ocean has incredibly affordable and straightforward pricing that we think you should take advantage of. Their servers start at only $5 per month. There’s also hourly pricing available for when you need to get that project done, that Sandbox or maybe that burst of traffic. Starting at less than a penny per hour but we’re going to make it so that you can get started today and deploy an SSD Cloud Server for free. Just visit DigitalOcean.com and create an account. Once you confirm your email and account information go to the billing section and enter the promo code C101 for a free $10 credit. That’s plenty to get started and explore what Digital Ocean can do. That’s digitalocean.com and once you sign up enter the code c101 in the billing section for a $10 credit. Now Lou, I like this whole bootstrap thing, this idea that you run the Rails command and it will build out all the dependencies including all the libraries and give you this nice play ground to work in. It seems like it is how every framework should work.

Lou: It really should.

Fr. Robert: Why don’t I see that more?

Lou: There’s actually a boot-strapper web framework that’s built like Java Script that does something very similar so they’re starting to learn from things like Ruby and Rails where they can bring in what they need at run time, even at compilation time. So this is kind of the key. They’re the ones that really started to do this.

Fr. Robert: 1 of the pieces that Carlos talked about which… I’ve been playing a lot since the pre-record and it’s absolutely true and that is; in order to get the efficiencies out of a framework you have to follow the conventions. It’s not optional anymore. It’s not like well I’ll follow the conventions when it’s good but if it’s going to take me awhile I won’t. If you don’t follow the conventions you might as well not follow the frame work. Like the example that we used here was having welcome as the folder for the controller. If you don’t do that none of the other hooks are going to work. Do you see a lot of that in other framework languages? Like when you’re working in C sharp and you’re working with the model and then view and the controller is there a convention that you’ve trained to use?

Lou: I think that’s a problem. A lot of times there are frameworks that come out with… they target 1 specific thing and if you don’t follow that 1 specific thing you move outside the Rails so to say. You start to lose the advantage of using that framework. But yes I think there’s a lot of times where if you have a framework that doesn’t have those conventions, they’re just very generic and they’re easier to use. But in this case you want to follow those conventions.

Fr. Robert: Yes, it’s sort of a 2 sided store because yes the bootstrap process really does cut down on the amount of scuttle work that you had to do but at the same time if the boot strap isn’t leading to what you want to do…and I’m spending a lot of time kind of pushing it back the other way, or you just break the conventions at which point you probably should be using the framework in the first place. What tips could you give because you are our permanent code warrior; we may have guest code warriors but you are permanent. What tips would you like to give the audience about the use of a framework? It doesn’t have to be Rails, it could be any framework that they’re going to pick up. As they go along how do they find the strengths and the weaknesses of each 1?

Lou: Like Ruby and Rails it’s a framework plus a tool kit. For instance some frameworks like in Dot net you have to go all the way in. You’re all in when it comes to a framework so sometimes when you’re choosing a framework if you feel you need to go all in that’s when you need to understand whether you want to take on that big role of going all in on a framework or not. There are some frameworks where you don’t need to go all in. There are some that you can say ok I’m only going to do part of this in Ruby and then part of this in something else. In that case you don’t have to learn everything or you don’t really have to understand everything. But in things like Java or C Sharp or even Ruby these are things that you sometimes have to go all in and understand everything from the top to bottom.

Fr. Robert: There you go. Go all in. Wise words from our permanent code warrior. Lou unfortunately that’s the end of this episode. Next week we’re going to be jumping straight back into Ruby on Rails. In fact Carlos Souza is going to be letting us into how we manipulate persistent data sets. So we’re finally going to get to real work. I mean it’s been a lot of set up and showing us how it works and how servers get set up automatically so we can test but now we have to try our hand at manipulating data and making sure we don’t break things as we’re programming. Until then though there’s a place that they can go if they want to find all of our episodes. Do you know where that is?

Lou: I think it is Twit.tv.

Fr. Robert: Yes just go to Twit.tv/code and there you’ll find all of our back episodes, all our back modules and you’ll also find a little drop down menu that will allow you to automatically have every episode of Coding 101 dropped into your device of choice each and every single week. If you want it on your iPhone you can do that and if you want it on your Android device we can do that. Your Mac, your PC, your laptop, your desk top, we’ve got you covered partner. It’s just because we love you. Yes that’s it. Also don’t forget that you can find us on Twitter. You can find me at twitter.com/padresj, that’s @padresj.

Lou: You can find me at @LouMM, LouMM.

Fr. Robert: If you follow us there you’ll find out who we’re going to be talking to each week and the language that we’ll be covering. It’s also a great place to ask us questions. If you’ve got questions about programming, questions about some of the topics that we’ve covered Twitter is a great place to go. Also you can jump into our Google Plus group. Just go to Google Plus and search for Coding 101. We’ve got lots of experts in there and lots of beginning programmers. So there are no stupid questions, there are only stupid people… no there are only trolls. But seriously it’s a great community to jump into and it’s actually a lot of fun to see people hash out different ways of solving a single problem.

Lou: I really love the competition they have on there.

Fr. Robert: Yes they are good. And you know what they are not all wacky, they will be critical of the show when they need to be critical so it’s a really good place to jump in and see whether or not we’re doing our job right. Now thanks to everyone who makes this show possible. Of course that’s to Lisa and to Leo for letting us do this show and for bringing on Lou as our full time co-host. Also special thanks to a man who sits behind the scenes; he pushes the buttons but he doesn’t get a lot of recognition. He’s the man, the myth, the Eskimo, Zach. Zach can you tell people where they can find you and your work?

Zach: Thank you Padre. You guys can find me on the Twitters @eskimozach.

Fr. Robert: Until next time you’ll find us every Monday 2:30 PM Pacific. I’m Father Robert Ballecer.

Lou: And I’m Lou Maresca.

Fr. Robert: And this has been Coding 101. End of line!

All Transcripts posts