諸行無常

IT色々お勉強中のブログ

2016-01-01から1年間の記事一覧

bootstrap-switch-railsを使う

radioボックスをかっこよくしたかったんで Gemfile gem 'bootstrap-switch-rails', '~> 3.3.1' application.js //= require bootstrap-switch javascripts設定 $("body.news").ready -> $('input:checkbox').bootstrapSwitch() あとはviewに普通に書くだけ、…

gem AASMを使う

gem AASMとは rubyに有限オートマトンを追加する為のもの(ステータス管理を簡単にするもの) class or moduleに定義する↓ #AASMをinclude include AASM #columnで対象のカラム、enum使う場合はenum:true aasm column: :hoge, enum: false do # 使うステー…

rails ファイルアップロードなんどやっても忘れるのでメモ

ジェネレータでup用クラス作成 rails g uploader Image つけた名前のあとにUploaderて名前がついたファイルが出来るっぽい モデルとアップデートファイルを結びつける mount_uploader :about_image1, ImageUploder mount_uploader :about_image2,ImageUplode…

メソッドの定義場所を調べる方法

methodで#Methodを取り出す (byebug) m=co.method(:is_entry?) #<Method: Hoge#is_entry?> (byebug) m.source_location ["(eval)", 1] (byebug) m.owner hoge.methods.grep /target/i hoge.methods - Object.methods</method:>

rails n+1解決のincludesの使い方すぐ忘れるのでまとめ

hoge has_many pogeの場合 Hoge.includes(:poges) 複数の場合 Hoge.includes(:poges,:ages) ネストされたリレーションの場合 Hoge.includes(:poges,ages:[:doge,boge]) //またはcomの下にtranslationsがある場合 includes(colb: [com: :translations]) inclu…

react学習中

constructor: (props)-> コンポーネントがマウントされる前に呼ばれる。コンストラクタを自分で実装したい場合は super(props) を必ず呼ばなければならない。状態を初期化する場所 propsはreadonly stateは変更可能 defaultはpropsとして自分自身の値を参照…

へなちょこエンジニア、railsでreact使ってみる

react jsx生成コマンド rails g react:component CommentBox react_componentというhelperメソッド react_component('CommentBox') 値を参照する時に使う this.props.hoge 値を変更する時に使う this.setState({ count: this.state.hoge + 1 }); refs divで…

nginx の自動起動覚書

linuxの場合 sudo chkconfig --add nginx sudo chkconfig nginx on macの場合 cp /usr/local/Cellar/postgresql/9.5.0/homebrew.mxcl.postgresql.plist ~/Library/LaunchAgents launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist

ruby例外処理捕捉について(slack)

railsで捉えたいexceptionの種類 library _builtin (Ruby 2.1.0) raiseの仕方 https://docs.ruby-lang.org/ja/latest/doc/spec=2fcontrol.html#raise エラーのgem GitHub - smartinez87/exception_notification: Exception Notifier Plugin for Rails rescue…

rails timezoneの扱いについて

いろいろ頭の整理の為 application.rbに設定すると変更できる ①表示時のタイムゾーンをJSTに変更 config.time_zone = 'Tokyo' ②DB保存時のタイムゾーンをJSTに変更 config.active_record.default_timezone = :local ②を設定してなかった時 yesterday = Time.…

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

まずslack側でwebhookの設定 https://creww.slack.com/apps/A0F7XDUAZ-incoming-webhooks add configurationボタンを押して通知したいslack channel選択 gem 追加 gem 'slack-notifier' slack側で取得したurlをhookに設定してtaskなどで以下を呼び出す hook …

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: bundl…

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.…

rails favicon.icoの設置場所

tools.dynamicdrive.com 上で画像を作る app/views/shared/_head.html.hamlに書く = favicon_link_tag '/favicon.ico 下記に配置 public/favicon.ico

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' #非同期処理するとき必要になるメールとか g…

railsでのflash使い方

基本 flash[:notice] = 'hoge' 現在のアクションのみ有効 flash.now[:notice] = 'hoge' 破棄 flash.discard(キー) flash[:notice]=nil

急にpostgresが繋がらなくなった

急にpostgresが繋がらなくなった psql: could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket "/tmp/.s.PGSQL.5432"? postmaster.pidを消したら何か動くようなった /usr…

bundle install で gem install nokogiriで失敗する

久しぶりにrailsアプリを作ろうとしたらnokogiriエラー いつも直し方を忘れるのでメモ エラー↓ -[2197]% bundle install Fetching gem metadata from https://rubygems.org/.......... Fetching version metadata from https://rubygems.org/.. Fetching dep…

負荷テスト、アラートテスト用

cpu負荷を上げる yes >> /dev/null ps aux | grep yes | grep -v grep top メモリ負荷上げる /dev/null < $(yes) free -m

ログインした後に元のページにリダイレクトする

DeviseManager::SessionsController protected def after_sign_in_path_for(resource) after_sign_in_path = session[:request_from] || manager_root_path session.delete(:request_from) return after_sign_in_path end ApplicationController def request…

Capistranoでstage環境のプロセスを起動停止する方法

ステージング環境に直接入ってコマンド打っても起動せぇへん bundle exec unicorn -c /var/www/hogehoge/current/config/unicorn/stage.rb -E deployment -D 停止はできるっぽい kill -QUIT `cat /var/www/hogehoge/current/tmp/pids/unicorn.pid` 入らんで…

CloudWatchでEC2を監視

標準だけだと足り無いのでカスタム用メトリックスを作成 [hoge@ip-2342-234 cloudwatch]$ ls credential custom_metrics.sh http_status_check.sh #!/bin/bash export JAVA_HOME=/usr/lib/jvm/jre export AWS_CLOUDWATCH_HOME=/opt/aws/apitools/mon export …

cap stage deployを実行するとPermission denied (publickey)というエラー

cap stage deployを実行するとエラーになる [git][* hoge-hoge-hoge]:~/projects/hoge/ bundle exec cap stage deploy 00:00 git:wrapper 01 mkdir -p /tmp/hoge/ ✔ 01 deployer@hoge.poge.me 0.226s Uploading /tmp/hoge/git-ssh.sh 100.0% 02 chmod +rx /t…

rails sidekiqでのエラーハンドリング

sidekiqでのエラーハンドリング config/initializers/sidekiq.rb Sidekiq.configure_server do |config| config.redis = { url: 'redis://127.0.0.1:3333' } end config.redisの下に以下を追加 config.error_handlers << Proc.new { |ex, ctx_hash| Exceptio…

rails enumの表示を共通化

viewでそれぞれ書くのがめんどくさかったんでmodelに定義 human_hoge ['hoge', 'hoge2', 'hoge3'].each do |method| define_method "human_#{method}" do I18n.t("activerecord.attributes.applicant.#{method.pluralize}").fetch(send(method).to_sym) end …

git でhttpsからgit接続に変更

公開鍵作成 cd ~/.ssh/ ssh-keygen -t rsa クリップボードにコピー pbcopy < ~/.ssh/id_rsa.pub github側で登録 https://github.com/settings/ssh app/.git/configを書き換え [remote "origin"] url = git@github.com:hoge/hoge.git 毎回パスワード聞かれな…

vagrant 使ってみる

vagrant up 失敗する cd .ssh/ chmod 600 authorized_keys

rsync の使い方

.git database.yml以外削除 ls | egrep -v '^.git|config/database.yml$'|xargs rm -r ファイルコピー rsync -a pogehoge/ ~/rails_projects/hoge --exclude '.git' --exclude 'config/database.yml'

postgres コマンド色々

import psql -f dump.sql hoge_development 制約削除 ALTER TABLE applicants DROP CONSTRAINT fk_rails_cd9891c05e; スーパユーザでログイン psql postgres ユーザ一覧 \du ユーザ作成 create user yourname; 権限付与 alter role yourname createdb; おま…

rails meta_tag設定

meta_tagを設定 :ruby set_meta_tags reverse: true, title: title || t('meta.title'), description: description || t('meta.description'), ArgumentErrorエラーが出る ArgumentError - wrong number of arguments (0 for 1): def description text = nil…