Ruby on Rails Workshop (for beginners): Create a To do list web app
Ruby on Rails Workshop: Intro >>
You will see that Ruby on Rails is a piece of cake.
Generating the basic files:
$ rails new todolist
Take a look in the structure created:
| +-assets # the front end
| | +-images
| | +-stylesheets
| +-controllers # parses user requests, data submissions, cookies, sessions
| +-helpers # here you can define methods to be use din your view, so you won't write logic where you only should have html
| +-models # Ruby classes: talk to the db, store and validate data, perform the business logic
| +-views # what your user sees, html.erb files. Read what the controller gives them
+-config # here you can define the routes
+-db # sqlite
+-lib # here you can save your rake tasks and modules
+-spec # Rails tottally supports tests: it already includes a test framework
Defining routes - in config/routes.rb uncomment the following line:
Yes, you got an error! Rails is looking for a Welcome controller that doesn't exist.
That's how a route is defined: /controller/action/id
So we need a controller and an action to this route.
$ rails new controller welcome
But there is no actions, so let's add the action:
Notice that Rails already created the welcome_helper for you.
class WelcomeController < ApplicationController
What the user will see? Nothing yet. Create a file with the name of the action in views/welcome: index.html.erb
Rails automatically render views with names that correspond to valid routes.
What is erb? ERB (Embedded RuBy) is HTML with Ruby code embedded in.
Add something to the view:
<%= "Welcome!!" %>
But we still need to make our To do list! Now that you go a taste of how controller, view and model are linked, we can use the easy way:
$ rails generate scaffold todo title:string description:text status:string
With this trick Rails generates for you all the files you need in model, view and controller. Take a look in the new files created by Rails.
A scaffold in Rails is a full set of model, database migration for that model, controller to manipulate it, views to view and manipulate the data, and a test suite for each of the above.
Now we need "to run" the migrations: runs the change or up method for all the migrations that have not yet been run.
$ rake db:migrate
Migrations are a convenient way to alter your database schema over time in a consistent and easy way.
So now we have a table in the database. What are we missing?
But don't worry, Rails already did that for you (of course!). Take a look in your config/routes.rb file:
With that Rails is able to link the view to all the actions that are in the controller. Now run:
$ rake routes
This commands shows you the available routes in your app.
- When you change the routes, in most of the cases, you need to restart the server to be able to see the changes.
How do we add gems? Let's try the gem "better_errors", it is helpful to debug your application.
Edit your Gemfile by adding the following line:
$ gem install better_errors
Now you can try it out by adding a proposital error. An error can look so much better with this gem.
You can read more about the gem here: https://github.com/charliesome/better_errors
And you can always look for new gems at http://rubygems.org
Gems are the way to go to improve your project.
A tip about that: before you start to code, while your planning your project, always look for gems that can help you with that. There are gems for everything.
Now you shoul dbe able to play with the code. You can try to add some style to the app by using CSS. :)
You can take a look at this link to start: http://guides.railsgirls.com/design/