This is for all you aspiring RoR developers on Windows who are just as dumb as I am. (But should be useful for other OSes as well)
If you're like me, your first instinct is to look for an existing IDE that will take care of all the configurations for you so that you can just say new->project and have a fully functional website.
And then you can right-click on your project and say new->table and get a nice wizard that asks what columns you want. And while you're at it, see all the nice and fancy things you can build into your site from the context menu, like views and controls etc...
Forget it. It doesn't exist. The best I've seen, I think, was Ruby Mine which successfully gave me a functional site after creating a new project, but it is absolutely useless in helping you learn from there.
Personally, I wanted to get to a point where I could undertand how it all worked: create a new website and modify it so I can have it do what I want. But today's ruby IDEs just add 20-50% of stuff to learn on top.
Even with the help of an IDE, you will have to learn how to use the command prompt. So if you don't already, you really should learn now.
Next, you will have to learn how ruby, gems and rails are installed. If you've already done some searching you'll come across this InstantRails thing that will claim to simplify things for you. But it only hides concepts that are actually fairly simple and critical for you to know:
RoR has 3 major components that need to be installed:
1. Ruby: It is an interpreted programming language. Ruby code is executed by running the ruby.exe executable.
You can install ruby from [here](http://rubyforge.org/frs/?group_id=167&release_id=5246). This tutorial was based on ruby 1.9.2.
2. Gems: AFAIK, gems is a tool that allows you to very easily download modules/components/applications written in ruby. Rails is one such component. And I expect that there are many other features that you can add to your Rails website using Gems.
Ruby 1.9.2 comes with RubyGems preinstalled.
To update gems, run "Start Command Prompt with Ruby" in start menu->all programs->ruby 1.9.2 p180, then type in:
gem update --system
3. Rails: The framework that will let you magically create websites effortlessly. To install it, execute the following in you ruby command prompt (you might want to make an easily available shortcut for that command prompt, you'll use it a lot):
gem install rails
Now you can finally create a website. Open up your command prompt. Create a new folder anywhere, like say c:\sites.
Then cd into sites. And type:
rails new MyFirstSite
cd MyFirstSite
There, now all the code for a very simple site has been created. You can try to start the site with the following command:
rails server
But it won't work because you are still missing components. Fortunately, the components required by the website are specified in c:\sites\MyFirstSite\gemfile. And all you need to do to install those components is to execute the following command:
bundle install
But that still won't work because the website uses sqlite3 and for some unfanthomable reason the dll doesn't get automagically downloaded like everything else. You can get it [here](http://www.sqlite.org/download.html) scroll down until you find the zip file for the sqlite dll. Unzip the dll and def file into your \lib\ruby\gems\1.9.1\bin\ directory if it exists or into \bin. I don't know why it must sometimes be in one folder and other times another folder.
Now, you may run the server command again (make sure you're in your site's root directory when you do):
rails server
Then, go to your favorite webbrowser and point it to http://localhost:3000, and you'll see your awesome new website.
You can shutdown your website by pressing ctrl-C in the command prompt.
But we're not done yet. You want to learn how to write new features for this awesome website.
If you have a favorite editor, such as notepad, feel free to use it. I recommend [Komodo Edit](http://www.activestate.com/komodo-edit/downloads), it's 100% free, open source.
Once installed, launch it, and go to project->new project. Browse to your website folder (c:\sites\MyFirstSite) and click save. And you'll have a great editor for your ruby code that will remain functional for way more than 30 days.
Rails follows the Model, View, Controller architecture. Which is super awesome. It also seems to expect you to create those using the "rails generate" command, which less awesome. Views and Controllers are really easy to make and you're better off knowing how to make them manually.
Models are a whole other story and if you're interested, the next paragraph tells you what I know about how Rails handles modifying models through migrations. But feel free to skip it because you don't really need to understand all that to get started.
When you've become rich thanks to the 10 million users in your database, you'll want to get even richer by adding new features. So you'll want to modify your database without losing anything in it, because that's where all your money comes from. Rails understands that and treats your database as the authoritative source for what your models look like. So when you do "rails generate model" it creates timestamped migration files in your db\migrate directory. And when you do "rake db:migrate" it asks the database if there are any migration files in db\migrate that it thinks it hasn't migrated to yet, if there, it runs them. And there's also db\schema.rb that's gets clobbered all the time which contains the actual definition of what your tables contain. But then there will be certain things that "rails generate" can't really do so you'll want to edit your migration files manually.
The point is, your life is complicated enough as it is. And your database isn't worth 10 million dollars yet. So instead of making the database + a bunch of migration files the authoritative definition of your models, we'll give that job to schema.rb
Unfortunately, it doesn't exist yet. So just this once, you will need to use the generate and migrate commands in the command prompt as follows:
rails generate model Zombie
rake db:migrate
This will create three files:
1. app\models\zombie.rb
2. db\schema.rb (woot!)
3. db\migrate\2011040200241_create_zombies.rb (you don't care about this one)
If you've done a bit of internet research you might have a clue as to where I'm going with this. That's right: [Rails for Zombies](http://railsforzombies.org). They give you a great intro on creating views and controllers as well as using models, views and controllers.
There's just two more things you need before you can take full advantage of what they teach:
1) You need to have your database match what their database will contain:
In your schema.rb file use these two tables:
create_table "tweets", :force => true do |t|
t.string "status"
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "zombies", :force => true do |t|
t.string "name"
t.string "graveyard"
t.datetime "created_at"
t.datetime "updated_at"
end
Your app\models\zombie.rb file is just fine as it is. Rails voodoo makes it so that you don't need to replicate the contents of the schema.rb file into the Zombie class.
But you do need to create the app\models\tweet.rb file. It should contain the exact same thing as zombie.rb except your class name should be "Tweet".
Next, you'll want to initialize the contents of those tables. In seeds.rb, paste the following:
Zombie.create([
{:name => "Ash", :graveyard => "Glen Haven Memorial Cemetary"},
{:name => "Bob", :graveyard => "Chapel Hill Cemetary"},
{:name => "Jim", :graveyard => "My Father's Basement"},
])
Tweet.create([
{:status => "Where can I get a good bite to eat?", :name => "Ash"},
{:status => "My left arm is missing, but I don't care", :name => "Bob"},
{:status => "I just ate some delicious brains.", :name => "Jim"},
{:status => "OMG, my fingers turned green. #FML", :name => "Ash"},
])
Then, update your database using the following commands in your command prompt
rake db:drop (deletes everything in the database)
rake db:setup (creates tables using schema.rb, then fills it with feeds.rb)
In lesson 2, rails for zombies will introduce relationships, so you'll need the following updated tweets definition (leave the zombies table as is):
create_table "tweets", :force => true do |t|
t.string "status"
t.integer "zombie_id"
t.datetime "created_at"
t.datetime "updated_at"
end
And you'll need to update the seeds for it as well (leave the Zombie initialization as is):
Tweet.create([
{:status => "Where can I get a good bite to eat?", :zombie_id => 1},
{:status => "My left arm is missing, but I don't care", :zombie_id => 2},
{:status => "I just ate some delicious brains.", :zombie_id => 3},
{:status => "OMG, my fingers turned green. #FML", :zombie_id => 1},
])
2) In your command prompt, go to your site's directory (c:\sites\MyFirstSite) and type:
rails console
This will give you a prompt that will let you execute ruby code inside your website's context. So you'll be able to do all the exercises in rails for zombies on your own computer using your very own website!
**Edit:** I recommend you do the rails for zombies tutorial first, then follow these instructions and then do the zombies tutorial again with your own server. To see results on your own server for Level 3 (views) you'll need to know the basics of controllers and routes (Levels 4 and 5).
Thanks! I have not read this long post yet but I am sure it will be very helpful to a guy who got frustrated by ruby but really wants to learn it.
A great guide to get started!
As a bonus: At one point you might want to finally release your first little app. *DO NOT* try to deploy it on your own root server. When I started out with rails a few years ago this nearly killed my motivation to ever touch rails again. Go with [heroku](http://www.heroku.com) as you won’t have to pay for your first dyno (process instance), anyway. This means you can deploy an app to the web comfortably within a few seconds (and it only takes a less than five miniutes to setup your app to work with heroku).
Disclaimer: This doesn’t mean you should give it a try at one point, but please don’t let it kill your motivation to learn ruby/rails, as it’s totally worth learning 🙂
Damn this was helpful.
http://ruby.railstutorial.org/ruby-on-rails-tutorial-book
This has worked pretty well for me. Just make sure you use http://rubyinstaller.org/ for Windows.
I fucking love you. I just started researching this language and figured reddit would have tips. Do you recommend any guides/books?