-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathrubyrails-getting-started.html
More file actions
212 lines (212 loc) · 10.8 KB
/
Copy pathrubyrails-getting-started.html
File metadata and controls
212 lines (212 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<meta charset="utf-8" />
<meta name="generator" content="pandoc" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
<title>SLP: Ruby on Rails: Getting Started</title>
<style>
code{white-space: pre-wrap;}
span.smallcaps{font-variant: small-caps;}
div.columns{display: flex; gap: min(4vw, 1.5em);}
div.column{flex: auto; overflow-x: auto;}
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
/* The extra [class] is a hack that increases specificity enough to
override a similar rule in reveal.js */
ul.task-list[class]{list-style: none;}
ul.task-list li input[type="checkbox"] {
font-size: inherit;
width: 0.8em;
margin: 0 0.8em 0.2em -1.6em;
vertical-align: middle;
}
.display.math{display: block; text-align: center; margin: 0.5rem auto;}
</style>
<link rel="stylesheet" href="../markdown.css" />
</head>
<body>
<main>
<h1 id="slp-ruby-on-rails-getting-started">SLP: Ruby on Rails: Getting
Started</h1>
<p><a href="index.html">Go up to the main SLP documents page</a> (<a
href="index.md">md</a>)</p>
<p>Ruby is the programming language, and Rails is the framework. They
are typically used at the same time, so the entire setup is often just
called “Rails”.</p>
<h3 id="install-ruby-and-rails">Install Ruby and Rails</h3>
<p>Note that Ruby is the programming language, and Rails is the
framework.</p>
<p>The correct versions of Ruby and Rails have already been set up
properly on the VirtualBox image and on the docker image.</p>
<p>If you are running this on your <em>own</em> machine, then you will
want to follow the instructions at <a
href="https://gorails.com/setup/ubuntu/16.04">https://gorails.com/setup/ubuntu/16.04</a>
if you are running Ubuntu 16.04. If you are running Mac OS X, that site
has directions <a href="https://gorails.com/setup/osx">here</a>.
Likewise, other versions of Ubuntu can be found from those two links. If
you are using another operating system, you are on your own (but note
that the versions <strong><em>MUST</em></strong> match what we are using
(Ruby 2.4.1 and Rails 5.1.3)). It’s okay if you have a more recent patch
level version (i.e., Ruby 2.4.2 or rails 5.1.4, for example), but you
can’t have a different minor or major version.</p>
<h3 id="setting-up-a-new-rails-app">Setting up a new Rails app</h3>
<p>To set up a new Rails app, you will need to follow the “final steps”
instructions from <a
href="https://gorails.com/setup/ubuntu/16.04">here</a>.
Specifically:</p>
<ol type="1">
<li>Run <code>rails new railshw -d mysql</code>, where “railshw” is the
name of the app you are creating
<ul>
<li>For this homework, your Rails app MUST be named “railshw”, and it
MUST be in your home directory, as this is how the web server is
configured. In particular, there must be a ~mst3k/railshw/public
directory.</li>
<li>For your project (when you are assigned to your project later), the
name of the Rails app should match your project tag</li>
<li>If it asks you for your password for sudo, hit Control-C. It wants
to install the Ruby gems (the libraries) system-wide, and we are going
to do it in your individual user account. To do this, enter
<code>cd railshw</code> to move into the Rails application directory
that you just created, then enter
<code>bundle install --path vendor/bundle</code>. This will take a
minute or two to complete.</li>
<li>If you running it through the docker image, then eliminate the
<code>-d mysql</code> – this will force it to be a SQLite3 database, and
you will transition to the MySQL database when you move to the course
server.</li>
</ul></li>
<li>Edit <code>railshw/config/database.yml</code>, and enter your MySQL
credentials (change username, password, and database). For now, change
<strong><em>all</em></strong> the fields (username and password appear
twice, and database appears three times). You can keep the same database
(the same name as your userid) for production and development (as this
is just a homework – you should <strong>NOT</strong> do that in
practice), and use a different database (such as
<code>mst3k_test</code>) for testing. For the homework, you can list
your password in plaintext in that file. However, for your project app,
you are <strong><em>NOT</em></strong> to have the plain text passwords
in the files in the repository – see the Security section, next, for how
to handle the password.</li>
<li>From the railshw/ directory, run <code>bundle install</code> (or, if
on the course server,
<code>bundle install --path vendor/bundle</code>).</li>
<li>From the railshw/ directory, run <code>rake db:create</code>. It
will say that ‘mst3k already exists’ or ‘mst3k_test already exists’,
once or twice – that’s fine.</li>
</ol>
<p>If you are running this on the course server, you will need to reload
the apache web server by running
<code>/usr/local/bin/reload-apache2</code>. At this point, you should be
able to view your Rails app at <code>http://server/rails/mst3k</code>.
Note that there is no tilde (“~”) there! And obviously replace “mst3k”
with your userid. It should look <strong><em>exactly</em></strong> like
the image at the bottom of this page.</p>
<p>If you are running this on your own machine, you may need to run
<code>gem install bundler</code> as root (as indicated <a
href="http://stackoverflow.com/questions/19061774/cannot-load-such-file-bundler-setup-loaderror">here</a>);
this was already done on the course server.</p>
<h3 id="security">Security</h3>
<p>There are a few values that must <strong><em>NOT</em></strong> be
kept in the repositories – they are the MySQL passwords in
config/database.yml, and the secret_key_base in config/secrets.yml. The
default files for config/database.yml and config/secrets.yml access
environment variables, but the Apache Passenger module doesn’t handle
those very well for individual users.</p>
<p>The easiest way is to copy secrets.yml to secrets.yml.template, and
like for database.yml to database.yml.template. Edit the actual files
(i.e., not the templates), and put the passwords or keys directly into
those files. Add the two *.template files via git, but do NOT add the
database.yml and secrets.yml files. Create a .gitignore file that lists
two lines, which are the names of those two files (database.yml and
secrets.yml).</p>
<p>What this does is commit to the git repo the tempaltes, but the
actual files – the ones with the passwords – are not kept in the repo.
This means, however, that each person who checks out the repo will have
to set those values manuall.</p>
<p>For those who are interested, there are more options listed <a
href="http://railsapps.github.io/rails-environment-variables.html">here</a>.
Keeping this information in environment variables, which is the
recommended way to handle this, causes conflicts with how Passenger
works (Passenger is the Apache module that runs your Rails app)</p>
<h3 id="installing-new-gems">Installing new gems</h3>
<ol type="1">
<li>Edit the Gemfile, and add the line to add the gem:
<code>gem 'foobar', group: :development</code>, or similar</li>
<li>Run <code>bundle install</code>
<ul>
<li>If you are on the course server,
<code>bundle install --path vendor/bundle</code> instead</li>
</ul></li>
<li>If you are running it locally, you should be able to view your app
through <code>rails server</code></li>
<li>If you are on the course server, reload the apache web server by
running <code>/usr/local/bin/reload-apache2</code></li>
</ol>
<h3 id="uploading-to-the-server">Uploading to the server</h3>
<ul>
<li>Upload the files to pegasus:
<code>rsync -a railshw/ mst3k@server:~/railshw</code>
<ul>
<li>The syntax for that command (such as all the forward slashes) must
be exact!</li>
</ul></li>
<li>Log into pegasus: <code>ssh mst3k@server</code></li>
<li>Move into the railshw/ directory that was just created when we
uploaded the directory: <code>cd railshw</code> and install the gems:
<code>bundle install --path vendor/bundle</code>
<ul>
<li>At this point, you should be able to view your application</li>
</ul></li>
</ul>
<p>If you see a Passenger error with “wrong ELF class” listed there,
then you are running a 32-bit machine locally, and the course server is
64-bit. In the project directory, run
<code>/bin/rm -rf vendor/bundle</code> and then reinstall the gems (as
above, with <code>--path</code>).</p>
<p>You may need to transfer the DB, or run db:migrate, depending on the
details of your app</p>
<h3 id="sqlite-to-mysql">SQLite to MySQL</h3>
<p>If you used the docker image, then you likely kept the data for the
app in a SQLite3 database. You can use the framework’s command to create
the tables. You can use the <a
href="http://www.sqlitetutorial.net/sqlite-dump/">.dump command</a> to
extract the data, and then enter it into the MySQL database on the
course server. If you are unsure what the format of the database
configuration file should be, create a new app (with a different name)
that uses MySQL, and look at the file therein for the format (or just
copy the database configuration file over).</p>
<p>It may be that the SQLite dump does not work well under MySQL. One
option is to edit the dump extensively, but that is long and annoying to
do. Another option is to migrate the DB through the framework on the
course server, and re-enter the data into the DB (from the tutorial,
copy and paste all the insert commands).</p>
<h3 id="troubleshooting">Troubleshooting</h3>
<p>If you get a Passenger error on the server that states, “wrong ELF
class”, as shown <a href="images/rails-bad-elf.png">here</a>, then the
problem is that you have installed all of your gems (via
<code>bundle install --path vendor/bundle</code>) on your local
<strong><em>32-bit</em></strong> machine or image, and then uploaded
them to the server, which is a 64-bit machine. To fix, you must delete
the <strong><em>entire</em></strong> bundle directory on the server:
<code>/bin/rm -rf ~/railshw/vendor/bundle</code>, and then reinstall
them all again (via
<code>bundle install --path vendor/bundle</code>).</p>
<h3 id="viewing-your-rails-application">Viewing your Rails
application</h3>
<p>If you want to view your rails application locally, you can view it
via the rails server, as described in <a
href="https://gorails.com/setup/ubuntu/16.04">those directions</a>
(basically, run <code>rails server</code>, and then view it at
<code>http://localhost:3000</code> in your favorite web browser).</p>
<p>Your Rails app will have a VERY specific directory that it must be
installed in, as the web server is already configured for that
directory. As long as it is in that directory, it will be visible
through Apache.</p>
<p>When you view your app, it should look
<strong><em>exactly</em></strong> like the image at the image below
(albeit with a different URL):</p>
<p><img src="images/rails-initial.png" /></p>
</main>
</body>
</html>