rails new catmosphere
cd catmosphere/
rails server
Or if you're lazy:
rails s
Will allow you to browse to http://localhost:3000 in your browser and see the newly created application. It should look something like this:
- Note: You have to keep the
rails s
terminal window open as long as you'd like the application to be up. As soon as you close it, you'll get a message from your browser saying that it couldn't connect.
There are a multitude of files and folders when you run rails new
. It takes time to learn them all, but the most important are the following:
/app/assets/
/app/controllers/
/app/models/
/app/views/
/config/
/db/
/Gemfile
The assets directory has three subdirectories, images/
, javascripts/
, stylesheets/
.
Rails gives you a bit of magic in that you can browse to your application at http://localhost:3000/assets/header.png
and it will know to look in /app/assets/images/header.png
or /public/header.png
to find the asset (this also works for javascript and stylesheets).
Images, like the name suggests, is a place for you to put all of the images that you plan on making available to your application. These could be logos, corny "About Us" stock photography, etc.
If you would like to display an image in your app/assets/images/
folder, you can do so with the image_tag
like so:
image_tag 'header.png'
Which will render the following:
<img alt="Header" src="/assets/header.png" />
Controllers can be thought of as the air traffic controllers of your application. They have the ability to connect different pieces of the MVC paradigm so that you can get a fully formed webpage. They are also set up in such a way that each method inside a class that inherits from ApplicationController
can be mapped to a public URI. We call public methods inside a controller class "Actions". This means you can have something like this in app/controllers/about_us_controller.rb
:
class AboutUsController < ApplicationController
def show
@founders = [ 'Brooks', 'Otto', 'Luna' ]
end
end
With a little bit of tweaking in your routes file and creating an appropriate view, you'll be able to browse to http://localhost:3000/aboutus
, and your view could render "Brooks", "Otto", and "Luna".
Controllers can be generated using the rails generate
or rails g
command:
rails g controller CatToys index show
Which will create:
class CatToysController < ApplicationController
def index
end
def show
end
end
It is your responsibility to fill in the code of these actions. For a base CRUD application, the code is usually the same.
Rails tries to follow a paradigm called "CRUD". It stands for the following:
Acronym | Definition | HTTP Verb |
---|---|---|
Create | When you create a new object | POST , PUT |
Read | When you read an object | GET |
Update | When you edit the object | PUT , PATCH |
Delete | When you destroy the object | DELETE |
And we typically map these to actions, as so for app/controllers/cat_toys_controller.rb
:
class CatToysController < ApplicationController
def index
@cat_toys = CatToy.all
end
def show
@cat_toy = CatToy.find(params[:id])
end
def new
@cat_toy = CatToy.new
end
def create
@cat_toy = CatToy.new(cat_toy_params)
if @cat_toy.save
redirect_to @cat_toy
else
render :new
end
end
def edit
@cat_toy = CatToy.find(params[:id])
end
def update
@cat_toy = CatToy.find(params[:id])
if @cat_toy.update_attributes(cat_toy_params)
redirect_to @cat_toy
else
render :edit
end
end
def destroy
CatToy.find(params[:id]).destroy
end
private
def cat_toy_params
params.require(:cat_toy).permit(:title, :description)
end
end
Params is a hash that is made available to you on every page request. While it's contents depend on the context of the action that was requested by the browser, two things are always made available to you; the controller and the index.
For example, when browsing to http://localhost:3000/cat_toys
(with the appropriate route), the following is in params:
{"action"=>"index", "controller"=>"cat_toys"}
You can also use query string parameters in the URL and have them accessed via the params hash as well. For example, when browsing to http://localhost:3000/cat_toys?admin=true
, the following will be in params:
{"admin"=>"true", "action"=>"index", "controller"=>"cat_toys"}