memorandum

IT技術の習得を記録します

itermでcontrol+rでコマンド履歴を検索する設定方法 

このコマンドを打った後に、

brew install peco

.zshrc以下に下記を追記する

function peco-select-history() {
    local tac
    if which tac > /dev/null; then
        tac="tac"
    else
        tac="tail -r"
    fi
    BUFFER=$(\history -n 1 | eval $tac | awk '!a[$0]++' | peco --query "$LBUFFER")
    CURSOR=$#BUFFER
    zle clear-screen
}
zle -N peco-select-history

bindkey '^r' peco-select-history

「Rails をはじめよう」をはじめる (2)

Rails をはじめよう | Rails ガイド

アプリケーションの実装と実行

config/routes.rbで articleリソース を宣言する

Rails.application.routes.draw do
 
  resources :articles
 
  root 'welcome#index'
end

Railsは「articles」というリソース名から単数形の「article」を推測し、両者をその意味にそって使い分けているという点です。prefix列で単一の項目には単数形のarticle、複数項目を扱う場合には複数形のarticlesが使われているという具合です。

・・・articleとついているものを呼び出してくれているようです

➜  blog bin/rails routes
Ignoring byebug-9.0.6 because its extensions are not built.  Try: gem pristine byebug --version 9.0.6
Ignoring debug_inspector-0.0.2 because its extensions are not built.  Try: gem pristine debug_inspector --version 0.0.2
      Prefix Verb   URI Pattern                  Controller#Action
    articles GET    /articles(.:format)          articles#index
             POST   /articles(.:format)          articles#create
 new_article GET    /articles/new(.:format)      articles#new
edit_article GET    /articles/:id/edit(.:format) articles#edit
     article GET    /articles/:id(.:format)      articles#show
             PATCH  /articles/:id(.:format)      articles#update
             PUT    /articles/:id(.:format)      articles#update
             DELETE /articles/:id(.:format)      articles#destroy
        root GET    /                            welcome#index

土台を設置

新規記事を作成するための場所がアプリケーション内に必要

ルーティングは既に定義されているので、リクエストはアプリケーションの/articles/newに送られます。ブラウザでhttp://localhost:3000/articles/newを開くと、今はルーティングエラーが表示されます。

このエラーが発生したのは、ルーティングで指定された先に、リクエストを処理するように定義されたコントローラが見つからない ではコントローラーを作成してみる

ArticlesControllerを作成

rails g controller articles

今度は、ArticlesControllerコントローラにnewアクションが見つからないというエラーが発生する コントローラ内でメソッドを定義する ArticlesControllerクラスの内側に以下のようにnewメソッドを作成

class ArticlesController < ApplicationController
  def new
  end
end

articles/newというテンプレートをArticlesControllerは探しにいく

ブログアプリケーションのようなシンプルなRailsアプリケーションでは、テンプレートの置き場所は1箇所ですが、複雑なアプリケーションではさまざまな場所にテンプレートが置かれることもあります。

テンプレートをapp/views/articles/new.html.erbに置くのが最もシンプル

app/views/articles/new.html.erb →拡張子の意味 1つ目の拡張子はテンプレートの フォーマット 2つ目の拡張子は使用されるハンドラー

app/views/articles/new.html.erbを作成して、その中に以下のように記入

<h1>New Article</h1>

f:id:IT_expertise_diary:20161210193412p:plain

最初のフォーム

フォームを始めるためには、form builder を使用する Railsにはform_forというヘルパーメソッドがあり、主にこれを使用してフォームを作成する

app/views/articles/new.html.erbを書き換える

#このフォームを識別するためのオブジェクトを渡す こでは:articleというシンボル
<%= form_for :article do |f| %>
# このメソッドのブロックの内側はFormBuilderオブジェクトを置きます(fで表すのが通例です)
 #タイトル
  <p>
    <%= f.label :title %><br>
    <%= f.text_field :title %>
  </p>
 #記事本文
  <p>
    <%= f.label :text %><br>
    <%= f.text_area :text %>
  </p>
 #fオブジェクトに対してsubmitを実行すると、フォームの送信ボタンが作成されます。 
  <p>
    <%= f.submit %>
  </p>
<% end %>

小さいブログを新規作成するフォームができた f:id:IT_expertise_diary:20161210194105p:plain

記入したブログの送信先を設定する app/views/articles/new.html.erbをエディタで開き、form_forの行を以下のように変更

<%= form_for :article, url: articles_path do |f| %>

bin/rails routesの結果

➜  blog bin/rails routes
Ignoring byebug-9.0.6 because its extensions are not built.  Try: gem pristine byebug --version 9.0.6
Ignoring debug_inspector-0.0.2 because its extensions are not built.  Try: gem pristine debug_inspector --version 0.0.2
      Prefix Verb   URI Pattern                  Controller#Action
    articles GET    /articles(.:format)          articles#index
             POST   /articles(.:format)          articles#create
 new_article GET    /articles/new(.:format)      articles#new
edit_article GET    /articles/:id/edit(.:format) articles#edit
     article GET    /articles/:id(.:format)      articles#show
             PATCH  /articles/:id(.:format)      articles#update
             PUT    /articles/:id(.:format)      articles#update
             DELETE /articles/:id(.:format)      articles#destroy
        root GET    /                            welcome#index

articles_pathヘルパーは、articlesという接頭語に関連付けられているURIパターンをフォームの送信先とするようRailsに指示

5.3 記事を作成する

ArticlesControllerコントローラ内にcreateアクションを作成し、フォームが動作するようにする app/controllers/articles_controller.rbファイル内のArticlesControllerクラス内のnewアクションの下にcreateアクションを追加

class ArticlesController < ApplicationController 
  def new
  end
 
  def create
  end
end

実際のパラメータがどのようになっているかを確認するために、createアクションに以下の変更を加える

def create
  render plain: params[:article].inspect
end

http://localhost:3000/articlesにはハッシュが表示された

<ActionController::Parameters {"title"=>"tetet", "text"=>"tetet"} permitted: false>

githubの公開鍵を作成しgithubに登録

Generating a new SSH key and adding it to the ssh-agent - User Documentation

#Open Terminal.

#Paste the text below, substituting in your GitHub email address.

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
#This creates a new ssh key, using the provided email as a label.

#Generating public/private rsa key pair.
#When you're prompted to "Enter a file in which to save the key," press Enter. This accepts the default file location.
#ここでなんの公開鍵がわかるようにid_rsa(ファイルの置き場所)の部分を書き換える
Enter a file in which to save the key (/Users/you/.ssh/id_rsa): [Press enter]
#At the prompt, type a secure passphrase. For more information, see "Working with SSH key passphrases".

#Enter passphrase (empty for no passphrase): [Type a passphrase]
#Enter same passphrase again: [Type passphrase again]
pbcopy < ~/.ssh/ファイルの置き場所pub

githubがどのsshを使用するか設定する .sshの中にconfigというファイルを作成し下記の内容を記載する

Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_github

「Rails をはじめよう」をはじめる (1)

railsguides.jp

アプリケーションの作成

rails new blog

で新しいRails アプリケーションの作成 場所は自分が作成したいディレクトリ内で実行

すると

cd blog

そこに移動

rails server

rails serverを起動

http://localhost:3000/にアクセスすると f:id:IT_expertise_diary:20161204181344p:plain

となる

Railsに"Hello"と挨拶させる

コントローラとビューが必要

コントローラは、アプリケーションに対する特定のリクエストを受け取って処理するのが役割です。 ルーティング は、リクエストをどのコントローラに割り振るかを決定するためのものです。 1つのコントローラに対して複数のルーティングがあるのはよくあることです。 そしてコントローラにはいくつかのアクション があります。 いくつかの異なるルーティングに対して、それぞれ異なるアクションを割り当てることができます。 それぞれのアクションは、情報を集めてビューに送り出すのが役割です。

ビューの役割は、この情報をユーザーが読める形式で表示することです。 ここで気を付けていただきたい重要な違いは、表示する情報を集めるのは コントローラであって、ビューではないということです。

ということ とりあえず、コントローラーからビューという流れはなんとなく理解 まずコントローラーが必要なので作成する

コントローラを新規作成

コントローラを新規作成するには、コントローラ用のジェネレータを実行します

# welcomeという名前のコントローラの中にindexというアクションを作成する
rails generate controller welcome index

Railsは指定どおりコントローラを作成し、関連ファイルやルーティングも設定してくれます

便利! 作られた場所もわかりやすく表示してくれます

      create  app/controllers/welcome_controller.rb
       route  get 'welcome/index'
      invoke  erb
      create    app/views/welcome
      create    app/views/welcome/index.html.erb
      invoke  test_unit
      create    test/controllers/welcome_controller_test.rb
      invoke  helper
      create    app/helpers/welcome_helper.rb
      invoke    test_unit
      invoke  assets
      invoke    coffee
      create      app/assets/javascripts/welcome.coffee
      invoke    scss
      create      app/assets/stylesheets/welcome.scss

welcomeコントローラ => app/controllers/welcome_controller.rb

indexビュー => app/views/welcome/index.html.erb に作成された

.erbがビューのようです

app/views/welcome/index.html.erbを開く

<h1>Welcome#index</h1>
<p>Find me in app/views/welcome/index.html.erb</p>

こんな形になっているのをすべて書き換え保存

<h1>Hello, Rails!</h1>

これでコントローラーとビューが用意された

アプリケーションのホームページを設定する

サイトのルートURL http://localhost:3000 にアクセスしたときにこのメッセージが表示されるようにするには Railsで表示させたい実際のホームページの場所を指定する

エディタでconfig/routes.rbを開く

config/はアプリケーションの設定ファイル (ルーティング、データベースなど) がここに置かれます。

先ほどでてきた、ルーティングはここで設定するようです

config/routes.rb ルーティングファイルの内容

Rails.application.routes.draw do
  get 'welcome/index'

  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end

外部からのリクエストをどのようにコントローラとアクションに振り分けるかを、DSL (ドメイン特化言語: domain-specific language) という特殊な言語を使用してこのファイル内に記述します。

Rails.application.routes.draw do
  get 'welcome/index'
  root 'welcome#index'
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end

root 'welcome/index' => アプリケーションのルートURLへのアクセスをwelcomeコントローラのindexアクションに割り当てるようRailsに指示が伝わる

get 'welcome/index' => http://localhost:3000/welcome/indexというリクエストをwelcomeコントローラのindexアクションに割り当てる

http://localhost:3000/welcome/indexを表示

f:id:IT_expertise_diary:20161204190534p:plain

まとめ:
# アプリケーションの作成
rails new blog
# そこに移動して
cd blog
# サーバーを起動し
rails server
# welcomeという名前のコントローラの中にindexというアクションを作成する
rails generate controller welcome index
# app/views/welcome/index.html.erb に表示させたい内容を記述
# config/routes.rb ルーティングファイルを設定