Creating a Rails App3:44 with Jay McGavren
Let's create a new Rails app, and then test it out.
Type "rails new", followed by the name of the app you want to create. To run a server, change into the new app's directory, then type "bin/rails server", or on Windows, type "ruby bin\rails server".
rails new appname cd appname bin/rails server
Rails will begin logging messages to your terminal. You should see a URL like "http://localhost:3000"; this is the address where the server is running. Paste that URL into your browser to view your app's welcome page.
To stop the server later, return to your terminal window and press Ctrl-C.
An important detail for Windows users: Anywhere in the upcoming courses that you see me type bin/rails, or bin/ followed by any other command, you need to change the command a little bit so that it works on your system. Just replace the
ruby bin\. You need to do this because, unlike Macs and Linux machines, Windows can't run Ruby scripts directly by default; they have to be run through the "ruby" command.
A new executable program named Rails should have been installed on your system 0:00 along with the Rails gem. 0:05 We're going to run that executable now to generate our app. 0:06 Type rails new, followed by the name of the app we want to create, blog. 0:09 This will create a new directory with the same name as the app, 0:15 make a whole bunch of files and directories inside that, and 0:17 then download and install some additional gems. 0:20 Our app is already set up to run and display a generic welcome page. 0:24 Let's try it out now, change into the new app directory with cd blog and 0:27 then type bin/rails server. 0:33 Or on Windows, type ruby bin\rail server. 0:41 Windows users, we'll talk more about why your command is different in a moment. 0:45 You'll see booting Puma. 0:50 Puma is the web server that Rails 5 runs on top of. 0:52 You'll also see Rails 5.0.0 applications starting in development 0:55 on localhost port 3000. 1:00 The important part there is the URL at the end. 1:03 http://local:3000 is the address where the server is running. 1:05 Localhost means it's on the local machine, your computer. 1:10 3000 is the port it's running on. 1:14 Multiple servers can run on the same computer as long as they take 1:17 connections on different ports. 1:19 A Rails application that's in development takes connections on port 3000. 1:21 Let's copy this URL and paste it into our web browser. 1:26 Your browser will connect to your own computer, localhost, and 1:32 Rails will respond with the welcome page. 1:35 Our app is running. 1:37 Now that we know the server works, let's shut it back down for the time being. 1:39 When Rails started, it also printed one other message, Use Ctrl-C to stop. 1:42 Those are the keys to press when you want to shut down the web server. 1:47 So return to your terminal window and press Ctrl+C to stop the server. 1:50 On the Windows, it may ask you to confirm, just type Y if it does. 1:54 I need to clear up an important detail about running Rails commands 1:59 before we go on. 2:02 Remember how I had you type bin/rails server or 2:03 ruby bin\rails server for Windows users? 2:07 Typing rails by itself on the command line will look for 2:12 an executable file named rails anywhere on your system. 2:15 This is fine if there's only one version of rails installed. 2:19 But if there's more than one version, 2:22 then it's possible to create a Rails app with one version of Rails and 2:24 run the server with a different version of Rails and the two versions conflict. 2:27 That's just a mess. 2:31 Typing bin/rails runs the Rails executable within the bin 2:33 subdirectory of your Rails app. 2:37 This ensures you're running the same version of Rails that your app 2:40 was created with. 2:43 Sometimes you can get away with typing rails by itself, 2:45 and you have to type rails by itself when creating a new app. 2:47 But to play it safe, 2:51 always type bin/rails followed by whatever command you're trying to run. 2:52 And now an important detail for 2:57 Windows users that I'm only going to mention this once. 2:59 Anywhere you see me type bin/rails or bin/ followed by any other command, 3:02 you need to change the command a little bit so that it works on your system. 3:08 Just replace the bin/, that's bin forward slash, with ruby bin\. 3:12 Ruby, space, bin, \. 3:18 You need to do this because unlike Macs and Linux machines, 3:21 Windows can't run Ruby scripts directly by default. 3:24 They have to be run through the ruby command. 3:28 So get in the habit of typing ruby bin\ and it'll quickly become second nature. 3:31 If you have trouble running Rails commands, check the teacher's notes for 3:37 some troubleshooting tips or ask the Treehouse Community. 3:40
You need to sign up for Treehouse in order to download course files.Sign up