Ruby on Rails !

Can you read my notes?!

RAILS!!!

Creating a new project:

Don't forget to commit often (every db:migrate) so we stay
updated with version control

$ rails new blarg

$ rails g model Tag name:string

$ rake db:migrate

$ rails g model Post content:text title:string written_at:datetime

$ rake db:migrate

$ rails g model PostTag post_id:integer tag_id:integer

$ rake db:migrate

The associations will be created but they will be blank. You
need to write them:

class Post < ActiveRecord::Base
has_many :post_tags
has_many :tags, through: :post_tags
end

class PostTaf < ActiveRecord::Base
belongs_to :tag
belongs_to :post
end

class Tag < ActiveRecord::Base
has many :post_tags
has_many :posts, through: :post_tags
end

-------------------------------------------------------------------

Use '$ rails c' to run the program without it being in production

-------------------------------------------------------------------

A lot of work is done in config/routes.rb

To see your routes in the command line run:

$ rake routes

Example route made in config/routes.rb:

get 'posts/:id' => 'posts#views'

^ Explained:
get 'posts/:id' => 'Controller#Action'

-------------------------------------------------------------------

Starting a server in rails

$ rails s


Then feel free to go to http://localhost:3000

-------------------------------------------------------------------

You will need to create your controllers:

$ touch app/controllers/posts_controller.rb

class PostsController < ApplicationsController
def view
# erb :view (sinatra)
# render :view (rails will look for a view.erb file)
@post = Post.find(params[:id])
end
end

Create a view file:

$ mkdir app/views/posts

Organize your views to the controller they go with

$ touch app/views/posts/view.html.erb

Rails already creates the html template! (using "Layouts")

<h1>Hello World!<h1>

<h2><%= @post.title %></h2>
<h3>Written on: <%=@post.written_at %></h3>

<%= simple_format @post.content %>

Rails already creates the html template! (using "Layouts")
Like what you read? Give Tucker Watts a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.