Phoenix select と radio_button

2018年5月21日 Posted by PURGE

■ selectタグの場合

  <div class="form-group">
    <%= label f, :gender, class: "control-label" %>
    <%= select f, :gender_cd, ["男性": "1", "女性": "2", "その他": "0"], class: "control-label" %>
    <%= error_tag f, :team %>
  </div>

■ radio button タグの場合

  <div class="form-group">
    <%= label f, :gender, class: "control-label" %>
    <%= radio_button f, :gender_cd, "1", checked: true, class: "control-label" %>男性
    <%= radio_button f, :gender_cd, "2", class: "control-label" %>女性
    <%= radio_button f, :gender_cd, "0", class: "control-label" %>その他
    <%= error_tag f, :team %>
  </div>

Phoenix date_select フォーマット

2018年5月21日 Posted by PURGE

  <div class="form-group">
    <%= label f, :birthday, class: "control-label" %>
        <%= date_select f, :birthday, default: :calendar.local_time(), builder: fn b -> %>
          <%= b.(:month, [
                  options: [
                    {gettext("1月"), "1"},
                    {gettext("2月"), "2"},
                    {gettext("3月"), "3"},
                    {gettext("4月"), "4"},
                    {gettext("5月"), "5"},
                    {gettext("6月"), "6"},
                    {gettext("7月"), "7"},
                    {gettext("8月"), "8"},
                    {gettext("9月"), "9"},
                    {gettext("10月"), "10"},
                    {gettext("11月"), "11"},
                    {gettext("12月"), "12"},
                  ]
                ]) %>
        <% end %>
    <%= error_tag f, :birthday %>
  </div>

Phoenix date_select 期間とデフォルト(本日日付)の設定方法

2018年5月21日 Posted by PURGE

  <div class="form-group">
    <%= label f, :birthday, class: "control-label" %>
    <%= date_select f, :birthday, default: :calendar.local_time(), year: [options: 1950..2030], class: "form-control" %>
    <%= error_tag f, :birthday %>
  </div>

Phoenix Framwork select tag

2018年5月21日 Posted by PURGE

■member_controller.ex

def new(conn, _params) do
  changeset = Portal.change_member(%Member{})
  teams = Portal.list_teams()
  render(conn, "new.html", changeset: changeset, teams: teams)
end

def edit(conn, %{"id" => id}) do
  member = Portal.get_member!(id)
  teams = Portal.list_teams()
  changeset = Portal.change_member(member)

  render(conn, "edit.html", member: member, changeset: changeset, teams: teams)
end

■new.html.eex / edit.html.eex

<h2>New Member</h2>

<%= render "form.html", Map.put(assigns, :action, member_path(@conn, :create)) %>

<h2>Edit Member</h2>

<%= render "form.html", Map.put(assigns, :action, member_path(@conn, :update, @member)) %>

■form.html.eex

<div class="form-group">
  <%= label f, :teams, class: "control-label" %>
  <%= select f, :team_id, Enum.map(@teams, &{&1.team_name, &1.id}) ,  class: "control-label" %>
  <%= error_tag f, :team %>
</div>

<div class="form-group">
  <%= label f, :gender, class: "control-label" %>
  <%= select f, :gender_id, ["男性": "1", "女性": "2", "その他": "0"], class: "control-label" %>
  <%= error_tag f, :team %>
</div>

PostgreSQL 基本的コマンド

2018年5月17日 Posted by PURGE

■ postgres ユーザでログイン

> psql -U postgres
ユーザー postgres のパスワード:
psql (10.4)
&quot;help&quot; でヘルプを表示します。

■ PostgreSQLのバージョン確認

postgres=# select version();
                          version
------------------------------------------------------------
 PostgreSQL 10.4, compiled by Visual C++ build 1800, 64-bit
(1 行)

■ データベース一覧表示

postgres=# \l
                                             データベース一覧
   名前    |  所有者  | エンコーディング |      照合順序      | Ctype(変換演算子)  |     アクセス権限
-----------+----------+------------------+--------------------+--------------------+-----------------------
 postgres  | postgres | UTF8             | Japanese_Japan.932 | Japanese_Japan.932 |
 template0 | postgres | UTF8             | Japanese_Japan.932 | Japanese_Japan.932 | =c/postgres          +
           |          |                  |                    |                    | postgres=CTc/postgres
 template1 | postgres | UTF8             | Japanese_Japan.932 | Japanese_Japan.932 | =c/postgres          +
           |          |                  |                    |                    | postgres=CTc/postgres
(3 行)

■ データベース作成

postgres=# create database sample_db;
CREATE DATABASE

■ データベース一覧表示

postgres=# \l
                                             データベース一覧
   名前    |  所有者  | エンコーディング |      照合順序      | Ctype(変換演算子)  |     アクセス権限
-----------+----------+------------------+--------------------+--------------------+-----------------------
 postgres  | postgres | UTF8             | Japanese_Japan.932 | Japanese_Japan.932 |
 sample_db | postgres | UTF8             | Japanese_Japan.932 | Japanese_Japan.932 |
 template0 | postgres | UTF8             | Japanese_Japan.932 | Japanese_Japan.932 | =c/postgres          +
           |          |                  |                    |                    | postgres=CTc/postgres
 template1 | postgres | UTF8             | Japanese_Japan.932 | Japanese_Japan.932 | =c/postgres          +
           |          |                  |                    |                    | postgres=CTc/postgres
(4 行)

■ データベース削除

postgres=# drop database sample_db;
DROP DATABASE
postgres=# \l
                                             データベース一覧
   名前    |  所有者  | エンコーディング |      照合順序      | Ctype(変換演算子)  |     アクセス権限
-----------+----------+------------------+--------------------+--------------------+-----------------------
 postgres  | postgres | UTF8             | Japanese_Japan.932 | Japanese_Japan.932 |
 template0 | postgres | UTF8             | Japanese_Japan.932 | Japanese_Japan.932 | =c/postgres          +
           |          |                  |                    |                    | postgres=CTc/postgres
 template1 | postgres | UTF8             | Japanese_Japan.932 | Japanese_Japan.932 | =c/postgres          +
           |          |                  |                    |                    | postgres=CTc/postgres
(3 行)

■ データベース作成 with ユーザ

postgres=# create database sample_db owner sample;
ERROR:  ロール"sample"は存在しません
postgres=# create user sample;
CREATE ROLE
postgres=# create database sample_db owner sample;
CREATE DATABASE
postgres=# alter role sample with password 'password';
ALTER ROLE
postgres=# alter role sample with createdb;
ALTER ROLE

Elixir 演算子

2018年3月23日 Posted by PURGE

基本的な演算子。
= は、パターンマッチ。 == は、弱い比較。 === == は、強い比較。

iex(1)> 1 = 1
1
iex(2)> 1 == 1 
true
iex(3)> 1 === 1
true
iex(1)> 1 = 1.0
** (MatchError) no match of right hand side value: 1.0
iex(2)> 1 == 1.0
true
iex(3)> 1 === 1.0
false

brunchでJSXコンパイルエラー

2018年3月9日 Posted by PURGE

Phoenixで、Reactを使用しようとしていて、BrunchでJSXのコンパイルエラーが発生。

error: Compiling of js/app.js failed. L29:12 Unexpected token 
     27 |     render(){
     28 |         return(
   > 29 |             <div className="App">
        |             ^
     30 |                 <div className="App-header">
     31 |                     <h2>Dash Board</h2>
     32 |                 </div>

Brunchの設定ファイルである、brunch-config.js に下記を追記したら動いた。

  // Configure your plugins
  plugins: {
    babel: {
      // Do not use ES6 compiler in vendor code
      presets: ["es2015", "react"],
      ignore: [/vendor/]
    }
  },

なかなかどうして。

nil の場合の値代入

2018年2月16日 Posted by PURGE

Ecto でDBから値を返す時、nilの場合がある。
この場合に、nilをreplaceしたい場合がある。その記述方法

result = %{total: nil}
total = result |> Map.get("total") || 0

この記述で、nil の場合には、0が代入される。

WindowsリモートデスクトップでCtrl+Alt+Delete

2018年2月1日 Posted by PURGE

Windowsの場合:[Ctrl] + [Alt] + [Delete]
Windowsリモートデスクトップの場合:[Ctrl] + [Alt] + [End]

Elixir モジュール組み立て方

2017年10月21日 Posted by PURGE


# Elixirモジュールの組み立て方
defmodule Return do
    # 外部I/F定義
    def return(list) do
        #Listで引数を構成する
        _return(list, 0)
    end

    # Retrun値定義
    defp _return([], result), do: result

    # Logic定義 
    defp _return([head|tail], result) do
        #head値を処理
        _return(tail, (head + result))
    end
end