Ruby on Rails
Menu Menuhttp://guides.rubyonrails.org/getting_started.html
これにそってやってみます。
まず、ruby on rails を install する。
もちろん、ruby を入れた状態で…
http://rubyonrails.org/download
sudo gem install rack sudo gem install rails rehash
アプリケーションを作る
rails new ~/src/rails/app1 cd ~/src/rails/app1デフォルトは sqlite3 。
server を動かしてみる
rails serverそこで、http://localhost:3000 へアクセスしてみる。
Hello
最小限の controller を用意する
rails generate controller home index
create app/controllers/home_controller.rb
route get "home/index"
invoke erb
create app/views/home
create app/views/home/index.html.erb
invoke test_unit
create test/functional/home_controller_test.rb
invoke helper
create app/helpers/home_helper.rb
invoke test_unit
create test/unit/helpers/home_helper_test.rb
invoke assets
invoke coffee
create app/assets/javascripts/home.js.coffee
invoke scss
create app/assets/stylesheets/home.css.scss
vi app/views/home/index.html.erb
<h1>Hello World!</h1>
に変更しよう。
デフォルトのページを消しておく
rm public/index.html
route を書き換える
vi config/routes.rb App1::Application.routes.draw do get "home/index"を
App1::Application.routes.draw do root :to => "home#index"に書き換える。
そこで、http://localhost:3000 へアクセスしてみる。
Hello World! と表示されれば Ok 。
Post を作る
rails generate scaffold Post name:string title:string content:text
vi db/migrate/*create_posts.rb
class CreatePosts < ActiveRecord::Migration
def change
create_table :posts do |t|
t.string :name
t.string :title
t.text :content
t.timestamps
end
end
end
というのがわかる。
migrate する
rake db:migrate
link を付加する
vi app/views/home/index.html.erb <!--- Find me in app/views/home/index.html.erb ---> <h1>Hello World!</h1> <%= link_to "My Blog", posts_path %>そこで、http://localhost:3000 へアクセスしてみる。
データを作る
リンクをたどって、New post の内容を入れて create Post ボタンを押す。
blog の構造を調べてみる
URLボタン
validation
vi app/models/post.rb class Post < ActiveRecord::Base endを
class Post < ActiveRecord::Base
validates :name, :presence => true
validates :title, :presence => true,
:length => { :minimum => 5 }
end
に変える。
rail console
する。
irb(main):001:0> p = Post.new(:content => "A new post")
=> #<Post id: nil, name: nil, title: nil, content: "A new post", created_at: nil, updated_at: nil>
irb(main):002:0> p.save
=> false
irb(main):003:0> p.errors
=> #<ActiveModel::Errors:0x007fdda9e564e0 @base=#<Post id: nil, name: nil, title: nil, content: "A new post", created_at: nil, updated_at: nil>, @messages={:name=>["can't be blank"], :title=>["can't be blank", "is too short (minimum is 5 characters)"]}>
irb(main):004:0> exit
すべての投稿を JSON で見る
http://localhost:3000/posts.json
コメント機能をたす
rails generate model Comment commenter:string body:text post:references
cat app/models/comment.rb
class Comment < ActiveRecord::Base
belongs_to :post
end
cat db/migrate/*create_comments.rb
class CreateComments < ActiveRecord::Migration
def change
create_table :comments do |t|
t.string :commenter
t.text :body
t.references :post
t.timestamps
end
add_index :comments, :post_id
end
end
db:migrate する
+leo+kono sqlite3 db/development.sqlite3
SQLite version 3.7.5
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> .schema
CREATE TABLE "posts" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(255), "title" varchar(255), "content" text, "created_at" datetime, "updated_at" datetime);
CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL);
CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version");
% rake db:migrate
== CreateComments: migrating =================================================
-- create_table(:comments)
-> 0.0033s
-- add_index(:comments, :post_id)
-> 0.0006s
== CreateComments: migrated (0.0041s) ========================================
% sqlite3 db/development.sqlite3
SQLite version 3.7.5
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> .schema
CREATE TABLE "comments" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "commenter" varchar(255), "body" text, "post_id" integer, "created_at" datetime, "updated_at" datetime);
CREATE TABLE "posts" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(255), "title" varchar(255), "content" text, "created_at" datetime, "updated_at" datetime);
CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL);
CREATE INDEX "index_comments_on_post_id" ON "comments" ("post_id");
CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version");
sqlite> ^D%
vi app/models/post.rb
で、comments の制約を書く
has_maney :comments
Route をたす
vi config/routes.rb App1::Application.routes.draw do resources :postsを
App1::Application.routes.draw do
resources :posts do
resources :comments
end
に変える。
コントローラーを作る。
rails generate controller Comments
create app/controllers/comments_controller.rb
invoke erb
create app/views/comments
invoke test_unit
create test/functional/comments_controller_test.rb
invoke helper
create app/helpers/comments_helper.rb
invoke test_unit
create test/unit/helpers/comments_helper_test.rb
invoke assets
invoke coffee
create app/assets/javascripts/comments.js.coffee
invoke scss
create app/assets/stylesheets/comments.css.scss
Add a comment を erb に追加する。
vi app/views/posts/show.html.erb
<p id="notice"><%= notice %></p>
<p>
<b>Name:</b>
<%= @post.name %>
</p>
<p>
<b>Title:</b>
<%= @post.title %>
</p>
<p>
<b>Content:</b>
<%= @post.content %>
</p>
<%= link_to 'Edit', edit_post_path(@post) %> |
<%= link_to 'Back', posts_path %>
を
<p class="notice"><%= notice %></p>
<p>
<b>Name:</b>
<%= @post.name %>
</p>
<p>
<b>Title:</b>
<%= @post.title %>
</p>
<p>
<b>Content:</b>
<%= @post.content %>
</p>
<h2>Add a comment:</h2>
<%= form_for([@post, @post.comments.build]) do |f| %>
<div class="field">
<%= f.label :commenter %><br />
<%= f.text_field :commenter %>
</div>
<div class="field">
<%= f.label :body %><br />
<%= f.text_area :body %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
<%= link_to 'Edit Post', edit_post_path(@post) %> |
<%= link_to 'Back to Posts', posts_path %> |
に変える。
このままだと、
NoMethodError in Posts#show
Showing /Users/kono/src/rails/app1/app/views/posts/show.html.erb where line #19 raised:
とか言われるので、comments method を contoller に追加する。
vi app/controllers/comments_controller.rb
def create
@post = Post.find(params[:post_id])
@comment = @post.comments.create(params[:comment])
redirect_to post_path(@post)
end
コメントを表示する
vi app/views/posts/show.html.erb
<h2>Comments</h2>
<% @post.comments.each do |comment| %>
<p>
<b>Commenter:</b>
<%= comment.commenter %>
</p>
<p>
<b>Comment:</b>
<%= comment.body %>
</p>
<% end %>