Blog 日記 の作成 (1) Ruby on Rails 入門

http://localhost:3000/ http://127.0.0.1/mysql/ http://127.0.0.1/phpmyadmin/
http://localhost:3000/account/signup
http://localhost:3000/account/login http://localhost:3000/account/logout
http://localhost:3000/entries/ http://localhost:3000/entries/new
http://localhost:3000/users/list
http://localhost:3000/comments/list

http://rubyfan.files.wordpress.com/2007/11/rubyfan0000591.jpg

秀丸エディタのインストール

秀丸エディタ

InstantRails-1.7-win.zipを C:\ にダウンロードして解凍する。
フォルダ構成
C:\InstantRails\rails_apps
InstantRails-1.7-win.zip

Instant Rails

解凍ソフト

ALZip 7.0 beta2

③C:\InstantRails\InstantRails.exeの起動
mysql - kill

④open ruby console window を開く

apacheの左のIを左クリック



⑤C:\InstantRails\rails_apps>rails rubyfan

と入力して[enter]

⑥データベースの作成
C:\InstantRails\rails_apps>cd C:\InstantRails\mysql\bin
C:\InstantRails\mysql\bin>mysql -uroot
mysql> CREATE DATABASE rubyfan_development default character set utf8;
mysql> CREATE DATABASE rubyfan_test default character set utf8;
mysql> CREATE DATABASE rubyfan_production default character set utf8;
mysql> exit;

⑦データベースのテーブル作成
C:\InstantRails\mysql\bin>cd C:\InstantRails\rails_apps\rubyfan
C:\InstantRails\rails_apps\rubyfan>ruby ./script/generate migration base



⑧C:\InstantRails\rails_apps\rubyfan\db\migrateの001_base.rb の編集

はてなの機能の一つ、「スーパーpre記法」の「シンタックス・ハ… - 人力検索はてな

class Base <ActiveRecord::Migration
 def self.up
 create_table :users do |t|
 t.column :login, :string
 t.column :password, :string
 t.column :name, :string
 t.column :mail, :string
 t.column :profile, :text
 t.column :created_at, :timestamp
 t.column :updated_at, :timestamp
 end
 create_table :entries do |t|
 t.column :title, :string
 t.column :picture_url, :string
 t.column :body, :text
 t.column :user_id, :integer
 t.column :comment_id, :integer
 t.column :postingdate, :date
 t.column :created_at, :timestamp
 t.column :updated_at, :timestamp
 end
 create_table :comments do |t|
 t.column :body, :text
 t.column :name, :string
 t.column :mail, :string
 t.column :url, :string
 t.column :entry_id, :integer
 t.column :created_at, :timestamp
 t.column :updated_at, :timestamp
 end
 end
 def self.down
 drop_table :users
 drop_table :entries
 drop_table :comments
 end
 end

⑨テーブルを作成
C:\InstantRails\rails_apps\rubyfan>rake migrate
テーブルを削除する場合
( C:\InstantRails\rails_apps\rubyfan>rake migrate VERSION=0 )

⑩足場造り
C:\InstantRails\rails_apps\rubyfan>ruby ./script/generate scaffold user
C:\InstantRails\rails_apps\rubyfan>ruby ./script/generate scaffold entry
C:\InstantRails\rails_apps\rubyfan>ruby ./script/generate scaffold comment

⑪login_generator のインストール
C:\InstantRails\rails_apps\rubyfan>gem install login_generator
C:\InstantRails\rails_apps\rubyfan>ruby ./script/generate login account
y [enter]



⑫日本語設定
Railsソーシャルブックマークを作ってみようか(第2回)

C:\Progra~1\Hidemaru\Hidemaru.exe C:\InstantRails\rails_apps\rubyfan\config\database.yml

development:
adapter: mysql
database: rubyfan_development
username: root
password:
host: localhost
### ここから encodingの前に半角スペース二個、utf8の前に一個ないと文字化けする
encoding: utf8
### ここまで

⑬C:\Progra~1\Hidemaru\Hidemaru.exe C:\InstantRails\rails_apps\rubyfan\config\environment.rb

### ここから
$KCODE = 'u'
### ここまで
# Be sure to restart your web server when you modify this file.

⑭C:\Progra~1\Hidemaru\Hidemaru.exe C:\InstantRails\rails_apps\rubyfan\app\controllers\application.rb

class ApplicationController < ActionController::Base
# Pick a unique cookie name to distinguish our session data from others'
session :session_key => '_rubyfan_session_id'

### ここから
before_filter :set_charset
protected
def set_charset
@headers["Content-Type"] = "text/html; charset=utf-8"
end
### ここまで

end

⑮login_generator の設定
C:\Progra~1\Hidemaru\Hidemaru.exe C:\InstantRails\rails_apps\rubyfan\app\controllers\application.rb

class ApplicationController < ActionController::Base
# Pick a unique cookie name to distinguish our session data from others'
session :session_key => '_weblog_rails_session_id'
### ここから
before_filter :set_charset
protected
def set_charset
@headers["Content-Type"] = "text/html; charset=utf-8"
end
### ここまで

### ここから
include LoginSystem
model :user
### ここまで
end



⑯C:\Progra~1\Hidemaru\Hidemaru.exe C:\InstantRails\rails_apps\rubyfan\app\controllers\entries_controller.rb

class EntriesController < ApplicationController
### ここから
before_filter :login_required, :except => [:show, :list]
### ここまで
def index
list
render :action => 'list'
end


⑰C:\Progra~1\Hidemaru\Hidemaru.exe C:\InstantRails\rails_apps\rubyfan\app\controllers\users_controller.rb
class UsersController < ApplicationController
### ここから
before_filter :login_required, :except => [:show, :list]
### ここまで
def index
list
render :action => 'list'
end



⑱manage rails applications の起動
rubyfan チェックして start with mongrel


http://localhost:3000/ http://127.0.0.1/mysql/
http://localhost:3000/account/signup
http://localhost:3000/account/login http://localhost:3000/account/logout
http://localhost:3000/entries/list http://localhost:3000/entries/new
http://localhost:3000/users/list
http://localhost:3000/comments/list

⑲C:\Progra~1\Hidemaru\Hidemaru.exe C:\InstantRails\rails_apps\rubyfan\app\models\user.rb

class User < ActiveRecord::Base
### ここから
#has_many :entries
#has_many :comments


#has_many :entries
#has_many :comments, :through=>:entries

has_many :entries
### ここまで
end

⑳C:\Progra~1\Hidemaru\Hidemaru.exe C:\InstantRails\rails_apps\rubyfan\app\models\entry.rb

 class Entry < ActiveRecord::Base
### ここから
#belongs_to :user


#belongs_to :user
#belongs_to :comment


belongs_to :user
has_many :comments
### ここまで
end

[21]C:\Progra~1\Hidemaru\Hidemaru.exe C:\InstantRails\rails_apps\rubyfan\app\models\comment.rb

 class Comment < ActiveRecord::Base
### ここから
#belongs_to :user


#has_many :entries
#has_many :users, :through=>:entries


belongs_to :entry
### ここまで
end

[22]C:\Progra~1\Hidemaru\Hidemaru.exe C:\InstantRails\rails_apps\rubyfan\app\controllers\entries_controller.rb
commentアクションの追加
createアクションとupdateアクションの編集

### ここから
 def comment
    @comment = Comment.new(params[:comment])
    @comment.entry_id = params[:id]
    if @comment.save
      flash[:notice] = 'Comment was successfully created.'
      redirect_to :action => 'list'
    else
      render :action => 'list'
    end
 end
### ここまで

def create
 @entry = Entry.new(params[:entry])

### ここから
 @entry.user = @session['user']
 ### ここまで

if @entry.save
 flash[:notice] = 'Entry was successfully created.'
 redirect_to :action => 'list'
 else
 render :action => 'new'
 end
 end

def update
 @entry = Entry.find(params[:id])

### ここから
 @entry.user = @session['user']
### ここまで

if @entry.update_attributes(params[:entry])
 flash[:notice] = 'Entry was successfully updated.'
 redirect_to :action => 'show', :id => @entry
 else
 render :action => 'edit'
 end
 end

[23]文字化け防止

C:\Progra~1\Hidemaru\Hidemaru.exe C:\InstantRails\rails_apps\rubyfan\app\views\entries\list.rhtml
文字コード UTF-8 改行 LF で保存

[24] C:\InstantRails\rails_apps\rubyfan\app\views\entries\list.rhtml の編集

はてなの機能の一つ、「スーパーpre記法」の「シンタックス・ハ… - 人力検索はてな

<hr>
<%= image_tag("http://www.rubyonrails.org/images/rails.png") %>
<h1>blog</h1><br>
<%= link_to "ruby マニュアル", "http://www.ruby-doc.org/core/" %><br>
<%= link_to "rails api", "http://api.rubyonrails.com/" %><br>

<hr>
<% unless @entries.empty? %>
<% user = User.find(@session['user'].id) %><br>
<% user.entries.reverse[0..4].each do |e| %>
<%# コメント user.entries.each do |e| %>

<%= "タイトル表示   #{e.title}" %><br>
<%= "ボディ表示   " + e.body %><br>
<%= "日時表示   " + e.created_at.to_s %><br>
<%= link_to 'Show', :action => 'show', :id => e.id %> 
<%= link_to 'Edit', :action => 'edit', :id => e.id %> 
<%= link_to 'Destroy', { :action => 'destroy', :id => e.id },:confirm => 'Are you sure?', :method => :post %><br>

<h2>Comments</h2>
<% for comment in e.comments %>
   <%= comment.body %><br>
<% end %>

<%= form_tag :action => "comment", :id => e.id %>
	<%= text_area "comment", "body" %><br>
	<%= submit_tag "Comment!" %>
<%= end_form_tag %>
   <hr>

<% end %>
<% end %>

<%= link_to 'New entry', :action => 'new' %>
<hr>
<%= unless @entries.empty?; @entries[0].attributes; end %>
<hr>
<%= unless @entries.empty?; @entries.methods; end %>

<hr>


[25] cssの編集
C:\Progra~1\Hidemaru\Hidemaru.exe C:\InstantRails\rails_apps\rubyfan\public\stylesheets\scaffold.css


body, p, ol, ul, td {
  font-family: verdana, arial, helvetica, sans-serif;
  font-size:   13px;
  line-height: 18px;
### ここから
    margin: 0; 
    padding: 2em; 
    background-color: rgb(255, 255, 245); 
    color: rgb(100, 100, 100); 
    line-height: 140%; 
    font-size: 95%; 

    border-color : rgb(110, 140, 130); 
    border-width : 2em; 
    border-style : solid;
### ここまで
}


[26] C:\InstantRails\rails_apps\rubyfan>rake stats
Ruby On Rails ピチカート街道 - 統計で丸裸にされそう。ぃゃぁん rake stats -


[27]C:\InstantRails\rails_apps\rubyfan>rake doc:app
Ruby On Rails ピチカート街道 - ruby クラス・メソッド仕様のドキュメント自動生成 -


[28]
http://localhost:3000/account/signup
http://localhost:3000/account/login http://localhost:3000/account/logout
http://localhost:3000/entries/

[29]参考文献


Ruby on Railsで一行掲示板を作成する (山本隆の開発日誌)
ruby による Web アプリケーションの構築
満足せる豚。眠たげなポチ。:Rolling with Ruby on Rails - Japanese Translation - p1
sample application blog の作成
Railsでブログを作ろう!(Creating a Weblog in 15 minutes) - hp12c
Four Days on Rails日本語版
Railsでソーシャルブックマークを作ってみようか(第2回)
Ruby on Rails | A web-application framework that includes everything needed to create database-backed web applications according to the Model-View-Controller (MVC) pattern.


RubyƒŠƒtƒ@ƒŒƒ“ƒXƒ}ƒjƒ ƒAƒ‹
Index of Files, Classes & Methods in Ruby 2.6.3 (Ruby 2.6.3)
http://api.rubyonrails.com/
るびま


一時間で覚える Ruby - MAYAH
Ruby の基本文法
ruby
exercise
るびま