Showing posts with label rails. Show all posts
Showing posts with label rails. Show all posts

Ruby on Rails - Success kinda!

Okay. I'm totally going to try this again, just to see if I can't figure it out.

Ruby on Rails - A ruby based framework for web stuff. Basically, I haven't a clue, but I'm going to at least try to get it running on Windows and make some rudimentary website things, just to see HOW easy it is once I get the basic syntax down.

For the steps outlined below, I'm basically following the tutorial from http://guides.rubyonrails.org/getting_started.html verbatim, save for a few edits I'm needing to make to get the libraries downloaded and installed properly (i.e., they never told me in that tutorial that we needed the Ruby DevKit installed, and the DevKit download page doesn't really say HOW to install it).

  1. Download and install Ruby. Current version is 2.0.0p247 from http://rubyinstaller.org/downloads/. I installed mine to C:\Ruby200.
  2. Ensure Ruby is accessible from anywhere in the command shell by typing "ruby -v". You should get the current version of Ruby. If not, you may need to edit your environment variable "PATH" to include C:\Ruby200\bin (edited, of course, to where you installed Ruby).
  3. Download and unzip ruby DevKit, again from RubyInstaller.org. For version 2.0.0 and up, use DevKit 4.7.2. MinGW64-32 or MinGW64-64 specifies whether you're on 32 or 64-bit. I unzipped the development kit to C:\Ruby_DevKit.
  4. Install Ruby DevKit:
    1. Open a command shell and navigate to the location where you unzipped the development kit (C:\Ruby_DevKit).
    2. Type "ruby dk.rb" init
    3. Type "ruby dk.rb" install
    4. You should get messages that the development kit overwrote a few ruby gems that may have been already installed (Updating convenience notice gem override for 'C:/Ruby200'), and an "installing /devkit.rb"
    5. Type "gem install rails". You should get a ton of secondary libraries (gems) downloaded along with the main rails gem and documentation for each which could take a good long while. Just be patient. You should get something like "20 gems installed" at the end of the sequence.
    6. Type "rails --version". I got the message "Rails 4.0.0"
    7. We now have the ruby libraries needed to build a Rails application. Create a new location for Rails (C:\Rails, perhaps), navigate into it from the command shell and type "rails new ". I did like the tutorial suggested and just tried "rails new blog" which created an entire app folder structure in the "blog" subfolder. This was after I received the success messages "Your bundle is complete!" after it had installed a bunch of ...whatevers to the blog folder.
    8. Now, I did notice, rails automatically installs its OWN webserver (WEBrick, not Apache or IIS), so this app is able to run straight from its own folder out of Rails. I believe there's a way to install it over top of Apache, and use the Apache server configurations to control its access, but that's for later. Right now I just want to get this thing working in the first place, and am willing to put up with WEBrick.
    9. So anyway, I'm now told to startup Rails by typing "rails server".
    10. Whoops! I got a bunch of sqlite errors. Looks like I need to set up database config and connections first. I also tried starting OUT of the blog application, in which case I was told I needed to be inside a Rails application folder before I could start the server.
    11. Well that was odd. Tried installing the sqlite3 gem, which did not fix anything. Turns out I needed some other version of Ruby that had all the headers included, which I apparently did not do by using the windows installer. I REALLY hate trying to compile gcc projects on Windows. Hopefully I can try to make it work with MySQL since I already have that installed with source files and it's a part of my path.
    12. Thus, I deleted the blog folder and re-created the app with "rails new blog -d mysql" which forces the new app to use MySQL instead of the default sqlite.
    13.  Got a bundler error that said I needed the mysql2 gem. Ran "gem install mysql2 '0.3.13'. This errored, so I tried "gem install mysql" which worked, but was not the right version.
    14. Finally after a bit of research, I got the mysql2 gem to work with the following
      1. "subst X: "C:\Program Files\MySQL\MySQL Server 5.6"
      2. "gem install mysql2 mysql2 --platform=ruby -- --with-mysql-dir=X: --with-mysql-lib=X:\lib --with-mysql-include=X:\include" <== Note the double -- in there. Basically, this splits out the main command with a bunch of sub-switches....I think. Simply put, you HAVE to have the -- --with-mysql- in there. Otherwise it will fail with an "invalid switch" error.
    15. Trying "rails new blog -d mysql" once more.
    16. Success! I received the "Your bundle is complete" message.
    17. However, when starting up the server, I once again got errors (needed MySQL2 gem 0.3.13). Tried to install using "gem install mysql2 --version=0.3.13 -- stuff stuff stuff". That worked. However, when trying to start up the server, I was STILL getting a bunch of stupid errors about missing something inside the mysql2 0.3.13 gem.
    18. I managed to fix this error. Basically, I was able to copy libmysql.dll from the MySQL Server/lib folder into Ruby/bin. Rails is now started up clean with no errors whatsoever -  I actually see the line "=> Ctrl-C to shutdown server."
    19. Tried navigating to "localhost:3000" and got the error "Specified 'mysql' for database adapter, but the gem is not loaded. Add `gem 'mysql'` to your Gemfile." - My database.yml I think is using mysql instead of mysql2 for its handle. I will change it there.
    20. Sure enough. Changing the "adapter" field in config/database.yml from "mysql" to "mysql2" got rid of the error. However, now it's attempting to login to my MySQL root user without a password. Gotta enter that information (with a password that you will not see repeated here ;)).
    21. Entered the password into database.yml. However, the error did not disappear. I manged to figure it out. I have to restart Rails. Ctrl-C in the command shell and run "rails server" again. This time it worked. However, now the website saying that it cannot find the "blog_development" database. Apparently it's not smart enough to create it itself (yet), so I will have to look up new tutorials on how to do that later on.
    22. Check that. I just went into PHPMyAdmin and created a blank "blog_developement" database, not adding anything to it. A refresh on the page now shows the "welcome to rails" homepage! I'm in!
In any case! I've apparently gotten it to work, so I'll be changing the title of the blog post from "A failed attempt" to "Success kinda!!"

Ruby on Rails for Windows

It was only recently that I FINALLY figured out just what Ruby on Rails is. From most of the posts I saw all I could make of it was "it's the thing that runs Twitter." Okay..so what's it DO?

Well as it turns out, from what I can make of it, Rails is a new web framework designed to streamline web design...BETTER than PHP/ASP/Javscript. So basically it's a new kind of webserver.

Well I decided that I wanted to try it out. Unfortunately, I don't run on Linux, and every time I do I get very frustrated. So I decided to try and get it going on Windows (XP to be exact). And I did it! Of course, I had to use an online tutorial (which is fairly outdated by about 2 years) located at All About Ruby on Wordpress.

And with a few modifications and updates to the main system, I was able to get it to work as is right now!

And this is as follows:

Install MySQL (or any other SQL engine, but I already have MySQL installed, so that's nice.)

Install Ruby (I had an old version 1.8.7, so I upgraded to 1.9.3p0. The tutorial says you'd also need to install gems, but I didn't have to. 1.9.x came with it pre-loaded. The installer for Windows is located at RubyInstaller. I installed to C:\Ruby\ (they recommended C:\Ruby193\, but I might want to upgrade. And what's next? Ruby194? Ruby200?).

I also made sure I included C:\Ruby\ in the windows path, and associated *.rb files. I also installed the TCL/TK addon, which I doubt I'll ever need, but hey, it's America where more is better!

One thing I also noticed later on in the installation was that I needed the optional DevKit located on the same page at RubyInstaller. At the time of this writing, the file is called DevKit-tdm-32-4.5.2-20110712-1620-sfx.exe. Otherwise the "gem" (a friendly name for ruby add-on methods).

The GitHub tutorial states that DevKit was written under MinGW. Now I'm not too happy with Linux frameworks in Windows (MinGW, Cygwin, etc), so I was a little worried that I'd have to install the actual framework. Not so! If you look at the GitHub tutorial, you'll see it has installing boiled down into three nice easy steps.

Unzip (from 7zip format) into a nice friendly Ruby-Dev folder (I chose C:\ruby-dev\) and navigate in via cmd window. By the way, I've learned it's never good to run ruby scripts from Windows Explorer. If it fails, the resulting command window closes automatically and you never see what's going on, so it's better to Start/Run/cmd.exe and navigate in via the DOS window.

Once in the Ruby-Dev directory, you could run
ruby dk.rb.

This by itself will just display a help screen with what you're actually supposed to do.


ruby dk.rb init
This generates a config.yml (a configuration file which in this case just tells the installer where the actual Ruby installation is located). You can open this config.yml in a text editor and see what's going on. There should be an entry near the bottom that says something like
- c:/ruby
or some such. If this line doesn't match your Ruby installation, change it so that it does. Remember you only want to go as deep as Ruby's top-level directory, not "bin" or anything deeper.

Next you run:
ruby dk.rb review
and get to see exactly what it's going to do based on your config.yml. It's not too detailed, so you could just skip this step and go straight to:
ruby dk.rb install


It worked for me, even when I did the confirmations listed in the Devkit tutorial on Github, but I won't tell you how to do that here. You can just read it for yourself (see the link above).

With this done, I could now begin the Rails installation. Remember rails is simply a new way of web developing. Basically, underneath all of the sparkles, it's nothing more than a webserver. Sure a webserver with a completely new way of writing the actual pages, but a webserver nonetheless.

According to Rails On Windows, you can now install the Rails gem. Remember gems are just add-ons for Ruby, much like you can install Android development kits to implement for the Java language. Except gems are just little default programs that can be called from any other program you write yourself to add functionality.

So anyway, install the Rails gem with dependencies included by running from the command window:
gem install rails --include-dependencies


If all goes well, the script should download all of the gems required to run rails out of the box, plus all of the documentation (ri and rDoc).

Once this is done, you'll be able to create a new directory for your rails project(s) (I put mine in C:\Rails), navigate there and run:
rails firstproject


This will set your project up and hopefully not stall out and crash when it tries to install JSON support (it did when I tried, but that's why I set up the DevKit detailed before, and after I did, it worked fine).

Once it's completed and you have received the 'finished successfully' message, you can now navigate into that project folder (cd firstproject), and run:
ruby ./script/rails server


Rails will now start up and you should see a few messages such as:

=> Booting WEBrick
=> Rails 3.1.1 application starting in development on http://0.0.0.0:3000
=>Call with -d to detach
=>Ctrl-C to shut down server
[2011-11-12 13:17:03] INFO  WEBrick 1.3.1
[2011-11-12 13:17:03] INFO  ruby 1.9.3 (2011-10-30) [i386-mingw32]
[2011-11-12 13:17:03] INFO  WEBrick::HTTPServer#start: pid=952 port=3000
It may take a while for all of these messages to appear, but once they have, you can now go into your browser and navigate to http://localhost:3000 and HOPEFULLY see a 'Congratulations and welcome to Ruby on Rails' or some such title screen.

That's the extent of THIS tutorial. I still have yet to figure out how to set up the database and tables and make it all come together that way, but at least I got the server up and running and displaying something nice in my browser!