諸行無常

IT色々お勉強中のブログ

rails timezoneの扱いについて

いろいろ頭の整理の為 application.rbに設定すると変更できる

①表示時のタイムゾーンJSTに変更 config.time_zone = 'Tokyo'

②DB保存時のタイムゾーンJSTに変更 config.active_record.default_timezone = :local

②を設定してなかった時

    yesterday = Time.current - 1.days

      .where('hoges.hoge_finish_at >= ?', yesterday.strftime("%Y-%m-%d 00:00:00"))

DBがUTCなので画面と表示してる時間だと取れない

      .where('hoges.hoge_finish_finish_at >= ?', yesterday.beginning_of_day)

でも以上のようにすると取れるようになった。railsで上手いこと変換してくれる? ちなみにutcに変換するには以下のようにする

yesterday.beginning_of_day.gmtime

rails slackにtaskでメッセージを送る

まずslack側でwebhookの設定

https://creww.slack.com/apps/A0F7XDUAZ-incoming-webhooks

f:id:babababand:20161026205652p:plain

add configurationボタンを押して通知したいslack channel選択

gem 追加

gem 'slack-notifier'

slack側で取得したurlをhookに設定してtaskなどで以下を呼び出す

      hook = "https://hooks.slack.com/services/XXXXXXXXXXXXXXXXX"
      if hook
        notifier = Slack::Notifier.new hook, http_options: { open_timeout: 2 }
        notifier.username = :dev
        begin
          notifier.ping "テストです。" 
        rescue
          # no need report problem with Slack
        end
      else
        Rails.logger.warn 'Slack hook is missing'
      end

以上

Capistrano エラーが出てデプロイ出来ない

  • bundle exec cap production deployすると以下のエラーが出る
実行bundle exec cap production deploy

      01 $HOME/.rbenv/bin/rbenv exec bundle install --path /var/www/zen/shared/bundle --without development test --deployment --quiet
      01 rbenv: bundle: command not found
(Backtrace restricted to imported tasks)
cap aborted!
SSHKit::Runner::ExecuteError: Exception while executing as hoge@99.11.11.11: bundle exit status: 127
bundle stdout: Nothing written
bundle stderr: rbenv: bundle: command not found

SSHKit::Command::Failed: bundle exit status: 127
bundle stdout: Nothing written
bundle stderr: rbenv: bundle: command not found

Tasks: TOP => deploy:updated => bundler:install
(See full trace by running task with --trace)
The deploy has failed with an error: Exception while executing as hoge@99.11.11.11: bundle exit status: 127
bundle stdout: Nothing written
bundle stderr: rbenv: bundle: command not found

** DEPLOY FAILED
** Refer to log/capistrano.log for details. Here are the last 20 lines:
  • サーバ側でbundlerをインストール
rbenv exec gem install bundler
  • 次はmigrateでエラー
01:58 deploy:migrate
      [deploy:migrate] Run `rake db:migrate`
01:58 deploy:migrating
      01 $HOME/.rbenv/bin/rbenv exec bundle exec rake db:migrate
      01 rake aborted!
      01 PG::ConnectionBad: FATAL:  Peer authentication failed for user "zen"
  • ユーザ作成してロール付与、データベース作ってみる
[hoge@ip-10-0-0-62 20161013121524]$ su - postgres
パスワード:
-bash-4.2$ psql postgres
psql (9.2.18)
Type "help" for help.
postgres=# create user zen;
CREATE ROLE
postgres=# alter role zen createdb;
ALTER ROLE
postgres=# \du
                             List of roles
 Role name |                   Attributes                   | Member of
-----------+------------------------------------------------+-----------
 postgres  | Superuser, Create role, Create DB, Replication | {}
 zen       | Create DB

postgres=# CREATE DATABASE hoge_production;
CREATE DATABASE

まだエラーが出る

 sudo find / -name "pg_hba.conf"
/var/lib/pgsql9/data/pg_hba.conf
sudo vim /var/lib/pgsql9/data/pg_hba.conf
local を peerからmd5に変更

sudo /sbin/service postgresql restart

00:13 deploy:migrating
      01 $HOME/.rbenv/bin/rbenv exec bundle exec rake db:migrate
      01 rake aborted!
      01 PG::ConnectionBad: fe_sendauth: no password supplied
sudo su postgres -c 'psql --username=postgres'
ALTER USER postgres with encrypted password 'paasss'

local を peerからtrustに変更したら動いた、多分セキュリティ的にダメなんかなぁ

text formとボタンをくっつけて表示する方法

text formとボタンをくっつけて表示する方法

input-groupで囲ってそん中にinput-group-btn or input-group-addon入れりゃいいっぽい

      .input-group
        = text_field_tag 'keyword',  class: 'form-control input-lg search-input'
        %span.input-group-btn
          %button.btn.btn-primary.btn-search
            %i.fa.fa-search

rails 初期gem導入

#haml導入
gem 'haml', '4.0.7
gem 'haml-rails', '0.9.0'

#bootstrap導入
gem 'bootstrap-sass', '3.3.6'

#論理削除できるやつ
gem 'paranoia', '2.1.5'

#findするとき便利なやつ
gem 'friendly_id', '~> 5.1.0'

#非同期処理するとき必要になるメールとか
gem 'sidekiq', '3.5.4'