Join Kevin Skoglund for an in-depth discussion in this video Terminal and Unix setup, part of Ruby on Rails 3 Essential Training.
As developers we are going to need to work with UNIX, which is the backbone of Mac OS X. We can't do that from the graphical user interface that you usually use on your Mac. Instead we will need to use a command-line application called Terminal. The Terminal application comes pre- installed on your Mac and it lives inside the Applications/Utilities folder. Where can you find that is you can either to File > New Finder Window, and select Applications or you may also have a quick link for Applications down here in your dock. Once you do that, scroll down to the bottom to find the folder called Utilities, open that up, and scroll down until you find the application Terminal.
I am also going to drag a copy of that to my dock just because we are going to be using the Terminal program very often, so that way I will have easy access to it. We can close this window and now I will launch Terminal. Now yours probably looks different from mine. For example, you probably have a white screen. That's simply because I've gone to Terminal > Preferences and I've changed the color settings here for mine to use a special set of configurations that I developed. I think that yellow on black is just a little easier on the eyes and it's clearer for you to read in these movies. You can feel free to play with the colors and set them however you like.
Now let's explore a little bit of UNIX. Don't worry that my command prompt here at the beginning looks different than yours. That's also something we can configure and you'll see that just in a moment. To begin with, let's start in the cd command. This is for Change Directory. This is what allows us to move between directories, the same way that we have open and close folders in the normal Mac OS. So in edition to telling it to change directory, we have to tell it where we want to go. First let's put in the tilde. That's in the upper left of your keyboard. That tilde is the shortcut for saying go back to my home directory.
That's the directory that I should be in by default but it's never a bad idea just to make sure that we are where we think we are. So this will change my directory to that directory. It doesn't give me any kind of prompt but that's where I am right now. If we type ls, that is a listing of what's in the directory. So now we can see what's in my directory and those are the same things that we would see if we went into the Mac and we, for example, open up a new window. Here's Kevin and here's those same folders. Desktop, Documents, Downloads, etcetera. So that's where we are. We are in my root user directory.
We can also change directories to another directory. So, for example, I can go into my Library folder by typing cd Library. Now I'll move into that Library folder. We can see that with another ls. I can then go backwards a directory by using cd.. and that will back me up one directory and now I'm back in my home directory. We can move forward more than one directory at a time. For example, I can type Library and then a slash and then whatever folder I want to go in. Let's say it's Application Support.
Another trick is that we can start typing what we want to hit the Tab key and it will auto complete for us, which is especially nice if a folder name has spaces in it, because then it puts the backslash in front of those spaces for us. So that will move forward into that Application Support folder. To go back now, cd../.. would go back to directories instead of going back just one. You get the idea. Now as I say we can ls to see what's in that directory. We can also pass in some options to ls. ls -la, we can do either one of those or both of those together.
The dash lets it know that it some options that we are going to be passing in and that will give us a list of all the things that are in the directory. It will also include the file permissions, the file owner, the file size, the date, and then you'll notice that the list of files that's there includes not only Desktop, Documents, Downloads etcetera, it also shows files that have dots in front of them. Those dot files are usually configuration files. They are hidden from our normal view on Mac OS but we can see them in UNIX when we pass in that -a option.
So ls -la will show us this list view and include those dot files. Okay let's try couple of other things here. I am just going to clear my screen with the Command+K, just to get that out of the way, and let's try echo 'hello', all right. So that's just simply going to echo back hello. Echo is just a real simple command that will return a value to us, just like an echo would. We can echo $SHELL, all capitals, and this is going to give us the value of the special constant that set up called SHELL.
When we are in UNIX, we are not just in UNIX. We are also in a working environment. By default on the Mac that's going to be the born again shell, which is just called bash. So most of the time you're going to be working in bash. It's kind of become the industry standard. Most people use bash. The people are using other ones usually use them because they were familiar with these other environments or because they're trying out some new ones that may have some fancy new features, but bash has really become very standard. So just keep in mind, you are going to be in UNIX working in the bash shell. Now we also have a command called which, and which will show us where a program is located.
Echo itself, it's just a tiny little program. It's not a big fancy program like Photoshop, and it exists inside the bin folder. There is a file called echo and that's the echo that's being called when I run echo. The reason why the location of that file is important is because it is possible to have more than one copy of an application on your computer, just like you can install more than one copy of a web browser. But since we are in the command line and we're just simply typing something like echo, it needs to know which one it should go and find. It's not like we are double-clicking on it, like we do from the graphical user interface.
So how does it know which one if we have more than one? Well the way it decides is by using another variable called PATH. This is my current path. What this is, is the places that will look for command and the order that it will look for them. So first it will look in usr/bin: to see if I have echo in there. If I don't, then next, it will they will look inside bin to see if it's there. Then it will look in usr/ sbin:/sbin:/usr/local/bin: and finally in this path here. Now notice that it got it on this second one here, /bin.
That's where it found echo. So it didn't find it in usr/bin:. It found it in just bin. This is very important. If UNIX isn't finding programs that it's looking for, it may be because your path does not include the path where that's located. It's not going to look anywhere else. It's going to look in these places that are in this list and then it's going to give up and say, "Sorry, I can't find it." So what we want to do now is actually make some changes to that path variable. Let's start by doing ls -la again. We will see the list of the files that are there. You will see that I have a file called bash_profile. I created that file.
You probably don't have it. If you do happen to have something called bash_profile, that's fine, or if you have something called .profile, those are basically the same things except bash_profile is just for the bash shell, whereas profile is for all shells. I typically tend use bash_profile just to make sure that it's clear that these of the instructions I want to use with bash. You don't have both unless you are really are certain about what you're doing because they can conflict with each other. So in order to open up bash_profile and edit it, I am going to use another command called nano. This is a built-in, very simple text editor from the command line, not like Microsoft Word or anything that's graphical like that,. This is from the command line, nano .bash_profile.
Now if you don't have this file, go ahead and type that anyway. That's fine. What we are going to do is say we are going to be creating and saving this file with this name. It's not like a lot of the other programs where you are used to, where you provide the name only after that fact. We are going to go ahead and give it the name at the beginning. So nano .bash_profile. Mine opens up and has some content in it already. The first one is telling it to customize my prompt. that's why my prompt looks different than yours. You can customize this yourself if you like. What I've said is, use the username followed by $ sign and the space.
You can Google for PS1 and find out more about how you can customize that if you want. Then I've also put in an alias here. So alias ll is going to be equal to this command. It's the same thing as ls -la that I have been typing, with H and G just to make it look a little prettier. So by defining this alias, now I can just type ll and get the same thing as if I typed all the rest of that out. ll is a very common one a lot of UNIX programmers use. I encourage you to use it as well. What I want to do though is skip down to the new line and this is the important part that you need to add, which is export PATH.
The path is now going to be equal to "/usr/local/bin:. That's the directory we first want to look in to see if we have anything installed. Then we are going to add /usr/local/sbin: and then after that, usr/local/mysql/bin and last of all if you don't find it in any of these places, look in that long list of places that you already had saved. Whatever value path had to begin with, stick those here.
So what I am basically doing is saying, "Hey, before you look in your default places, check a few of my custom folders first." This usr/local is where we are going to be storing things and that's a very common way to do it so that all of your programs that you install, will all be installed in your usr/local folder. They will be kept there and separate from everything else. So let's do that. We will hit Return one last time there just to make sure that we have one extra line return and then you will notice that down here, it tells me to Exit and I am going to hold down the Control key. That's what that up arrows letting me know, so Control+X.
Save the modified buffer? We are going to tuck a Y because yes, we want to save it. File name we want write to, we have already indicated its bash_profile so it has that in there. We just hit Return and now we've saved the changes to that file. Another UNIX command is cat, which will output the contents of a file. cat .bash and we can also use auto- complete here, bash_profile, hit Return and now we should see the contents and they should look something like this. Again PS1 and alias are optional, you don't have to have those, but export PATH is the important part.
You definitely want to make sure you have it and you want to make sure that it looks just like what I have there. Now if we try again the echo $PATH, you will notice that we don't get our new changes. That's because the profile gets run whenever a new Terminal window opens. So let's close this window and let's just open up a new window again. Here we are. Now if we say echo $PATH, you will see that we do get our changes. They are included there, and also because I use the ll for the alias, you will see that I can just type ll now and get that full list.
Okay, so that does it or configuring UNIX and learning a few of the basics about how to move around. Next we need to move onto installing some other software.
- Understanding MVC (Model View Controller ) architecture
- Routing browser requests through the framework
- Responding to requests with dynamic content
- Defining associations and database relationships
- Creating, reading, updating and deleting records
- Working with forms
- Validating form data
- Reviewing built-in security features
- Authenticating users and managing user access
- Debugging and error handling
Skill Level Beginner
Q: When running the AlterUsers migration as described in the "Migration methods" video, I am getting "rake aborted!" with an "Invalid Date: BTREE" error. What could be causing this error?<br />
A: add_index is causing the problem. There appears to be in a bug in either the MySQL2 gem or in the MySQL lib file. Some users have reported that using the libmysql.dll file from MySQL 5.1 (32-bit) will fix the problem. The simplest fix is to comment out that line in the migration. Your code will still work; not having an index on the column will just slow down some database lookups.<br />
<span style="font-family: Tahoma, Geneva, sans-serif; font-size: 13px; ">Q: When I try to open up the server (WEBrick) by typing "rails server", as shown in the movie "Accessing a project", I receive the following error:</span> <div><span style="font-family: Tahoma,Geneva,sans-serif; font-size: 13px;"><br /> </span><span style="font-family: Courier; font-size: 10pt;">Gem::Specification#default_executable= called from /Library/Ruby/Gems/1.8/specifications/rubygems-update-1.8.3.gemspec:11./Library/Ruby/Gems/1.8/gems/mysql2-0.3.2/lib/mysql2/mysql2.bundle: dlopen(/Library/Ruby/Gems/1.8/gems/mysql2-0.3.2/lib/mysql2/mysql2.bundle, </span><span style="font-family: Courier; font-size: 10pt;">9): Library not loaded: libmysqlclient.18.dylib (LoadError)</span> <div><span style="font-family: Courier; font-size: 10pt;">Referenced from: /Library/Ruby/Gems/1.8/gems/mysql2-0.3.2/lib/mysql2/mysql2.bundle<br /></span><span style="font-family: Courier; font-size: 10pt;"> Reason: image not found - /Library/Ruby/Gems/1.8/gems/mysql2-0.3.2/lib/mysql2/mysql2.bundle</span> </div> </div>
<span style="font-family: Tahoma, Geneva, sans-serif; font-size: 13px; ">The installation problem on Mac OS X Snow Leopard is likely caused by a bug in the mysql2 gem that appeared when MySQL 5.5 came out. Hopefully newer versions of MySQL or the mysql2 gem will fix them problem. Until then, a detailed solution to the problem can be found at <a href="http://freddyandersen.wordpress.com/2010/10/03/mysql-5-5-snow-leopard-and-rails" target="_blank">http://freddyandersen.wordpress.com/2010/10/03/mysql-5-5-snow-leopard-and-rails</a>.</span>
<div>Q: While performing the steps outlined in the "Migration methods" video, I'm receiving an error. The rake db:migrate works fine, but rake db:migrate VERSION=0 results in the following error:</div> <div> <font><font style="font-family: Tahoma,Geneva,sans-serif; font-size: 10pt;"><br /> <span style="font-family: Courier; font-size: 10pt;">rake aborted!</span><br /><span style="font-family: Courier; font-size: 10pt;"> An error has occurred, all later migrations canceled:</span><br /> <br /><span style="font-family: Courier; font-size: 10pt;"> Index name 'index_admin_users_on_username' on table 'admin_users' does not exist.</span><br /> </font></font> </div>
<div>A: To isolate the error, comment out that line and any others that are resulting in errors. <font style="font-family: Tahoma, Geneva, sans-serif; font-size: 10pt">Then try again. Once you get to VERSION 0, then uncomment them again. </font></div> <div> </div> <div><font><font style="font-family: Tahoma, Geneva, sans-serif; font-size: 10pt">This is also explained in the movie "Solving migration problems" later in the chapter.</font></font> <br /> </div>
Q: I'm OS X 10.6.7 and there's a problem with RubyGems 1.8.1 and the database won't start. Should I somehow delete RubyGems and use and earlier version? How doing I get out of this pickle and start again to complete the course? The error message in Terminal reads:<br /><br /><span style="font-family: Courier; font-size: 10pt;">NOTE: Gem::Specification#default_executable= is deprecated with no replacement. It will be removed on or after 2011-10-01.</span><br /><span style="font-family: Courier; font-size: 10pt;">Gem::Specification#default_executable= called from /Library/Ruby/Gems/1.8/specifications/rubygems-update-1.8.1.gemspec:11.</span><br /> <br /><span style="font-family: Courier; font-size: 10pt;">user$ pwd</span><br /><span style="font-family: Courier; font-size: 10pt;">/Users/user/Sites/simple_cms</span><br /><span style="font-family: Courier; font-size: 10pt;">user$ rails server</span><br /><span style="font-family: Courier; font-size: 10pt;">NOTE: Gem::Specification#default_executable= is deprecated with no replacement. It will be removed on or after 2011-10-01.</span><br /><span style="font-family: Courier; font-size: 10pt;">Gem::Specification#default_executable= called from /Library/Ruby/Gems/1.8/specifications/rubygems-update-1.8.1.gemspec:11.</span><br /><span style="font-family: Courier; font-size: 10pt;">NOTE: Gem::Specification#default_executable= is deprecated with no replacement. It will be removed on or after 2011-10-01.</span><br /><span style="font-family: Courier; font-size: 10pt;">Gem::Specification#default_executable= called from /Library/Ruby/Gems/1.8/specifications/rubygems-update-1.8.1.gemspec:11.</span><br /><span style="font-family: Courier;">NOTE: Gem::Specification#default_executable= is deprecated with no replacement. It will be removed on or after 2011-10-01.</span>
A: The first section, with "default_executable= is deprecated", is just a bunch of annoying warning messages. A lot of people in the Rails community are annoyed about it.<br /><br />First try: <span style="font-family: Courier; font-size: 10pt;">gem pristine --all --no-extensions</span><br /><br />If that doesn't fix it, then you can go back to the less-noisy version of RubyGems until all those other gems get updated to remove the code causing the warnings, using: <span style="font-family: Courier; font-size: 10pt;">sudo gem update --system 1.7.2</span><br /><br />The second part is the actual error:<br /> <br /><span style="font-family: Courier; font-size: 10pt;">dyld: lazy symbol binding failed: Symbol not found: _mysql_get_client_info</span><br /><br />This is usually because you installed the wrong version of MySQL (32-bit vs. 64-bit).
Q: I'm trying to create a new subject, as shown in the Chapter 7 movie "Creating new records." I encounter a problem when I input:<br /> <br /><span style="font-family: Courier; font-size: 10pt;"> this subject = Subject.new9:name => "Forth Subject", :visible => true)</span><br /> <br /> I get the error message ActiveModel::MassAssignmentSecurity::Error: Can't mass-assign<br /> protected attributes. What is the issue here?
A: Starting with Rails 3.2.3, released just this week, mass assignment security is turned on by default. (See <a target="_blank" href="http://weblog.rubyonrails.org/2012/3/30/ann-rails-3-2-3-has-been-released/">http://weblog.rubyonrails.org/2012/3/30/ann-rails-3-2-3-has-been-released/</a> and <a target="_blank" href="http://guides.rubyonrails.org/security.html#mass-assignment">http://guides.rubyonrails.org/security.html#mass-assignment</a> for more information.)<br /> <br /> You can deal with this change in one of two ways:<br /> <br /> 1. Turn off the security setting. Open config/application.rb and change config.active_record.whitelist_attributes to false instead of true. This makes your app a little less secure, but allows you to quickly move forward with the tutorial.<br /> <br /> 2. Work with the security settings. The proper technique is to go into each model (like Subject) and add attr_accessible for each field that a web form should be able to mass assign a value to. For example:<br /> <br /> <span style="font-family: Courier; font-size: 10pt;">class Subject < ActiveRecord::Base</span><br /> <br /> <span style="font-family: Courier; font-size: 10pt;"> attr_accessible :name, :visible</span><br /> <br /> <span style="font-family: Courier; font-size: 10pt;"> end </span>
Q: When trying to create or connect to a database, I'm getting a "cannot login to localhost" error message. What's the solution?
A: As mentioned in the Chapter 4 "Accessing a project" video, localhost is an alias for the 127.0.0.1 ip address. If you have any problems connecting to localhost, default to using the IP address instead. Assign the host value in the database.yml file to the ip 127.0.0.1 instead of localhost.
Q: This course was updated on 8/12/2013. What changed?
A: We updated 12 movies to reflect changes to Ruby on Rails, as of version 3.2, and to accommodate the release of version 4.0. This includes updates to the Macintosh installation process, how you access a new project, loading stylesheets, and making model attributes accessible for mass assignment.