Hello, World!

難しいことは書けません

gitからcloneしてきたRailsアプリを動かしたい

gitからcloneしてきたRailsアプリを動かすにはどうしたかをまとめた(前半少しぬけてる気がする)

cloneしてきてlocalhostで見れるまで

まずRailsアプリをcloneしてきて、cloneしてきたディレクトリに移動し、server起動

git clone git@github.com:username/app_name.git
cd app_name
rails s

しかし
can't find gem bundler (>= 0.a) with executable bundle (Gem::GemNotFoundException) ~~
とエラーがでる
調べると、とりあえず以下のコマンドを打てばいいのかな?
参考:can't find gem bundler (>= 0.a) with executable bundle 対応 - Qiita

bundle install --path vendor/bundle

今度はTraceback (most recent call last): ~~~ とエラー
原因はGemfile.lockに記載されているBUNDLE WITH(1.17.1)のバージョンが、インストールされているbundlerのバージョンと異なっていることらしい
参考:can't find gem bundler (>= 0.a) with executable bundle 対応 - Qiita

確認すると今インストールされてるバージョンは 2.0.1で
f:id:eeko-amaryllis:20190204142034p:plain:w600
今回のRailsアプリのGemfile.lockを見ると1.17.1
f:id:eeko-amaryllis:20190204142203p:plain:w600
なので、それにバージョンを合わせるために再インストールするコマンドを実行

gem install bundler -v 1.17.1

特にエラーも出ずうまくいったので、以下のコマンド実行

bunble install

これもまたうまくいったかんじなので、server起動しhttp://localhost:3000を確認すると、画面に大きくActiveRecord::PendingMigrationError とエラーが出たのでmigrateするため以下のコマンドを実行
参考:ActiveRecord::PendingMigrationError と言われた時の解決方法 - Qiita

bundle exec rake db:migrate 

これでcloneしてきた環境で動かすことができる

どうにかして楽にbladeファイルを作成したい

LaravelでWebサービスを作成した際にbladeファイルを作るのがすごくめんどくさかった
いちいち以下のようなコマンドを打っていた

mkdir -p resources/views/shop
mkdir -p resources/views/menu

touch resources/views/shop/index.blade.php
touch resources/views/menu/list.blade.php

めちゃくちゃめんどくさい

なのでテキストファイルを読み込んでそこに書かれているディレクトリとbladeファイルを作ってくれるコードを書いた
shellで書いてもいいのだけど、なんとかくPerlで書いてみようと思い書いてみた

file_list.txt
resources/views/shop/index.blade.php
resources/views/shop/shop_information.blade.php
resources/views/shop/shop_introduction.blade.php
resources/views/shop/shop_other.blade.php
resources/views/menu/sort.blade.php
resources/views/menu/list.blade.php
resources/views/menu/add.blade.php
resources/views/menu/edit.blade.php
resources/views/menu/delete.blade.php
resources/views/staff/sort.blade.php
resources/views/staff/list.blade.php
resources/views/staff/add.blade.php
resources/views/staff/edit.blade.php
resources/views/staff/delete.blade.php
make_file_dir.pl
#!/usr/local/bin/perl

my $make_dir_command = 'mkdir -p';
my $make_file_command = 'touch';
open(DATAFILE, "< file_list.txt") or die("error :$!");

if ($?) {
  print "エラーが発生しました。\n";
  print "$make_file_command:$!", "\n";
} else {
  while (my $data = <DATAFILE>){
    chomp($data);

    if ($data =~ /\//) {
      foreach my $row_data($data){
        my $line = $row_data;
        #末尾から/以降を削除
        $row_data =~ s/\/[^\/]*$//;
        system("$make_dir_command $row_data");
        system("$make_file_command $line");
        print "--実行コマンド--\n";
        print "$make_dir_command $row_data\n";
        print "$make_file_command $line\n";
      }
    } else {
      print "/が含まれていません。\n";
    }
  }
}

実行結果

$ perl make_file_dir.pl
--実行コマンド--
mkdir -p resources/views/shop
touch resources/views/shop/index.blade.php
--実行コマンド--
mkdir -p resources/views/shop
touch resources/views/shop/shop_information.blade.php
--実行コマンド--
mkdir -p resources/views/shop
touch resources/views/shop/shop_introduction.blade.php
--実行コマンド--
mkdir -p resources/views/shop
touch resources/views/shop/shop_other.blade.php

... 省略 ...

もしかしたら初めのif文はいらないかもしれないけど一応
テキストファイルのデータが変数$dataに入っていて、foreachでデータ1行1行(resources/views/shop/index.blade.php などが)変数row_dataに入ってる
そして、一旦そのデータを変数lineに保持
次に変数row_dataの末尾の"/"以降の部分(index.blade.phpなど)を削除する
ここで変数row_dataにはディレクトリ名だけの値が入っている(例:resources/views/shop)
system() で指定したコマンドを実行
(参考:http://www.tohoho-web.com/perl/cmd.htm
ディレクトリを作成している

$make_dir_command $row_data

ディレクトリが先に作られているので、ファイルを作成している

$make_file_command $line

これでできた!

シェルでMySQLを実行し、csvファイルに書き出す

descでカラム一覧を表示して、その結果をcsvファイルに出力したかった。
けどそのファイル出力を50回しないといけなくてとても面倒なのでシェルでかいてみた。
columns_list.txtにはカラム名が書かれているテキストファイル。
以下シェルプログラム

#!/bin/bash

while read columns
do
  sql="desc $columns"
  mysql -uroot -proot -e "$sql" db_name > ~/columns_csv/$columns.csv
done < columns_list.txt


書き出せたけどWarningがでる。

[Warning] Using a password on the command line interface can be insecure.

/Applications/MAMP/conf/my.cnfで以下のプログラムを追加したけど、Warningは消えなかったなぁ🤔

[client]
user = root
password = root
host = localhost


参考
【シェルスクリプト】ファイルの中身を一行ずつ読み込む方法 | server-memo.net

MySQL5.6のUsing a password on the command line interface can be insecureの対応 - 文系プログラマによるTIPSブログ

NeoBundleのインストール

iTermの設定をしようと思ったらNeoBundleが必要だった

NeoBundleとは?

vimプラグイン管理ツール
アップデートも自動で行えたり、プラグインの追加や管理が効率的にできるもの?らしい

以下のサイト通りやるとうまくいった
github.com

コマンドうってく

1. Install NeoBundle

install.shをcloneして、実行する

$ curl https://raw.githubusercontent.com/Shougo/neobundle.vim/master/bin/install.sh > install.sh
$ sh ./install.sh
2. Setup NeoBundle:
$ mkdir ~/.vim/bundle
$ git clone https://github.com/Shougo/neobundle.vim ~/.vim/bundle/neobundle.vim

cloneできた
f:id:eeko-amaryllis:20180622141330p:plain:w500

しかしここでvimってコマンドうつと、こんな感じなものが表示される
f:id:eeko-amaryllis:20180622141336p:plain:w500

のでもうひと押し

3. Configure bundles:

さっきのサイトを参考に.vimrcを書いてく
あと.vimrcと.zshrcのファイルをgithubに管理しておく際にここを参考にすると良い
qiita.com


そしてコマンドでvimを開き、:NeoBundleInstallを叩くとok
f:id:eeko-amaryllis:20180622141347p:plain:w500

c++でのちょっとした書き方の違い

先ほどのsortのプログラムで少し書き方が違うのがあったのでメモ

#include <iostream>
#include <list>
#include <string> //追加
#include <algorithm>  // std::sort, std::unique 追加
#include <vector>     // std::vector 追加
using namespace std;

//void print (list<string> &list1) {
void print (std::vector<string> &list1) {
  for (auto x: list1) {
    cout << x << " ";
  }
  cout << endl;
}

int main(int argc, char const* argv[])
{
  //list<string> list1 = { "ACC","ACC","CCW","CWA","WAC","CCT","CTT","TTT" };
  std::vector<string> list1 = { "ACC","CCW","CWA","ACC","WAC","CCT","CTT","TTT" };

  cout << "入力された文字" << endl;
  print(list1);

  //list1.sort();
  //list1.unique();
  std::sort(list1.begin(), list1.end());
  list1.erase(std::unique(list1.begin(), list1.end()), list1.end());
  cout << "After sort&unique list1: ";
  print(list1);

  return 0;
}

c++で文字列をsortとunique

複数の文字列を重複を削除して、アルファベット順に並べるプログラム

#include <iostream>
#include <list>
#include <string>
using namespace std;

void print (list<string> &list1) {
  for (auto x: list1) {
    cout << x << " ";
  }
  cout << endl;
}

void print (list<int> &list2) {
  for (auto x: list2) {
    cout << x << " ";
  }
  cout << endl;
}

int main(int argc, char const* argv[])
{

  list<string> list1 = { "ACC","CCW","CWA","ACC","WAC","CCT","CTT","TTT" };
  list<int> list2 = {1,1,1,1,1,2,3,1,2,3,2,8,9};

  cout << "入力された文字" << endl;
  print(list1);
  print(list2);

  list1.unique();
  cout << "After unique list1: ";
  print(list1);

  list2.unique();
  cout << "After unique list2: ";
  print(list2);
  cout << endl;

  list1.sort();
  list1.unique();
  cout << "After sort&unique list1: ";
  print(list1);

  list2.sort();
  list2.unique();
  cout << "After sort&unique list2: ";
  print(list2);

  return 0;
}

実行結果

$ g++ -std=c++11 test.cpp
$ ./a.out
入力された文字
ACC CCW CWA ACC WAC CCT CTT TTT
1 1 1 1 1 2 3 1 2 3 2 8 9
After unique list1: ACC CCW CWA ACC WAC CCT CTT TTT
After unique list2: 1 2 3 1 2 3 2 8 9

After sort&unique list1: ACC CCT CCW CTT CWA TTT WAC
After sort&unique list2: 1 2 3 8 9

list1で"ACC","CCW","CWA","ACC","WAC","CCT","CTT","TTT"の並びだとunipueされないが、"ACC","ACC","CCW","CWA","WAC","CCT","CTT","TTT"の場合ACC二つが隣り合ってるので片方は削除される


参考サイト
http://kaworu.jpn.org/cpp/std::list::unique
http://kaworu.jpn.org/cpp/auto#Range_based_for_.2B_auto_.E5.9E.8B.E3.81.AE.E4.BE.8B
https://qiita.com/ysk24ok/items/30ae72f4f1060b088588

あとついでに
https://dixq.net/forum/viewtopic.php?f=3&t=5601
https://qiita.com/hal1437/items/b6deb22a88c76eeaf90c

WordPressで複数のサイトを作る

WordPressで複数のサイトを作る際に、新しく作ったサイトにアクセスするとnot foundとでたのでそのときの解決法をメモ

以下のサイト参考に作成
usortblog.com

調べたらAllowOverride NoneをAllにするとか、.htaccessの設定がおかしいという記事を目にするけどどれもきちんとできていたので何故かな〜と思っていたらmod_rewriteが原因だった

httpd.confのmod_rewriteコメントアウトされていたから!!
f:id:eeko-amaryllis:20171117140816p:plain:w500
これを有効化して、

#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
# Options FileInfo AuthConfig Limit
AllowOverride None

上記のところをAllowOverride Allに変更し、Apasheを再起動するとできた!

sudo apachectl restart

なんだ〜〜


以下参考サイト
centoshome.seesaa.net