1.Project 作成 rails new..
— rails new blog
   rails new blog –skip-bundle (バンドルをインストールする必要が無い場合はスキップする)
— JavaScript Runtime  
   Gemfileを編集 #gem ‘therubyracer’, : platform => :ruby コメントアウトする。
— rake db:create  初期化
— Browserで確認

2. Post (Model/DB/URL/Controller/View)
— rails g model Post title:string content:text rake
 ※ models/post.rb の作成
— db:migrate
— rails g controller Posts ※コントローラーの作成
※ controllers/posts_controller.rb と views/posts/ が作成
 – config/routes.rbを編集
  resources :posts

3.Comment (Model/DB/URL/Controller/View)

その他
■rails console
データベースに書き込める
例 
p = Post.new(:title => ‘my first post’, : content => ‘hello!’)

p.save ※記憶する
exit ※終了
■rails dbconsole (rails db)
データの中身を見る
.tables ※テーブル
select * from posts;  ※ データ
.exit ※終了

[Rails上のリンクの書き方]

home
<%= link_to 'home', '/' %>

■views/posts/index.html.erbの編集

Contents

Posts

    <% @posts.each do |post| %>

  • <%=link_to post.title, post_path(post) %>
    <%=link_to 'Edit', edit_post_path(post) %>
  • <% end %>

<%=link_to 'home', posts_path %> | <%= link_to 'New', new_post_path %>

[テンプレート・CSSの編集]
■views/layouts/application.html.erb ⇔ テンプレート

<%= yield %> の中に、views/posts/index.html.erbが挿入されるイメージ。

■assets/stylesheets/application.css ⇔ アプリケーション全体のCSS

■assets/images にロゴを入れるのが主流 (views/layouts/application.html.erb)
<%= image_tag 'logo.png', :size=> ‘200×40’ %>
<%= image_tag 'logo.png', :size=> ‘200×40’), ‘/’ %>  ※リンク入り

[作成画面をrootにする]
■config/locales/routes.rb に加える
root :to => ‘posts#index’

※public/ に標準画面がある

Show ⇔ Postの詳細をあらわす app/views/poasts/show.html.erb /posts/(ID)
*rake routesで確認。
$ rake routes
(in C:/Users/Daisuke/Documents/Aptana Studio 3 Workspace/Daisuke01/blog)
Prefix Verb URI Pattern Controller#Action
posts GET /posts(.:format) posts#index
POST /posts(.:format) posts#create
new_post GET /posts/new(.:format) posts#new
edit_post GET /posts/:id/edit(.:format) posts#edit
post GET /posts/:id(.:format) posts#show
PATCH /posts/:id(.:format) posts#update
PUT /posts/:id(.:format) posts#update
DELETE /posts/:id(.:format) posts#destroy
root GET / posts#index

■Showを作成
※app/controllers/posts_controller.rbにshowの部分を追加

class PostsController < ApplicationController def index @posts= Post.all end def show @post = Post.find(params[:id]) end end ※app/views/poasts/show.html.erbのソース

<%= @post.title %>

<%= @post.content %>

<%= link_to 'home', posts_path %>