おぴよの気まぐれ日記

おぴよの気まぐれ日記

岡山やプログラミング、ファッションのこと、子育てや人生、生き方についての備忘録。

Gemfile通りにherokuが'bundle install'してくれない!

何が起こってるのか

herokuではどうやら、 sqlite3は使えないそうなので本番とローカル環境でDBを切り分けるようGemfileを更新しました。

# Gemfile
source 'https://rubygems.org'

gem 'rails', '~> 5.0.0', '>= 5.0.0.1'
gem 'pg'
gem 'puma', '~> 3.0'
gem 'sass-rails', '~> 5.0'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.2'
gem 'jquery-rails'
gem 'jbuilder', '~> 2.5'

group :development, :test do
  gem 'byebug', platform: :mri
  gem 'sqlite3'
end

group :development do
  gem 'web-console'
  gem 'listen', '~> 3.0.5'
  gem 'spring'
  gem 'spring-watcher-listen', '~> 2.0.0'
end

gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

developmenttestでは、sqliteを。 productionでは、pgを使うように修正しgit push heroku masterを実行!

だがだがだが、何度やってもsqliteをインストールしてきて失敗する。 bundle updatebundle exec bundle installやってもダメ。

そのエラーがこちら(長いけどログ全部です)

$ git push heroku master
Counting objects: 106, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (94/94), done.
Writing objects: 100% (106/106), 23.17 KiB | 1.78 MiB/s, done.
Total 106 (delta 5), reused 0 (delta 0)
remote: Compressing source files... done.
remote: Building source:
remote:
remote: -----> Ruby app detected
remote: -----> Compiling Ruby/Rails
remote: -----> Using Ruby version: ruby-2.3.4
remote: -----> Installing dependencies using bundler 1.15.2
remote:        Running: bundle install --without development:test --path vendor/bundle --binstubs vendor/bundle/bin -j4 --deployment
remote:        Warning: the running version of Bundler (1.15.2) is older than the version that created the lockfile (1.15.4). We suggest you upgrade to the latest version of Bundler by running `gem install bundler`.
remote:        Fetching gem metadata from https://rubygems.org/.........
remote:        Fetching version metadata from https://rubygems.org/..
remote:        Fetching dependency metadata from https://rubygems.org/.
remote:        Fetching rake 12.0.0
remote:        Fetching concurrent-ruby 1.0.5
remote:        Fetching i18n 0.8.6
remote:        Installing i18n 0.8.6
remote:        Installing rake 12.0.0
remote:        Installing concurrent-ruby 1.0.5
remote:        Fetching minitest 5.10.3
remote:        Fetching thread_safe 0.3.6
remote:        Installing minitest 5.10.3
remote:        Installing thread_safe 0.3.6
remote:        Fetching builder 3.2.3
remote:        Fetching erubis 2.7.0
remote:        Installing builder 3.2.3
remote:        Fetching mini_portile2 2.2.0
remote:        Fetching rack 2.0.3
remote:        Installing mini_portile2 2.2.0
remote:        Installing erubis 2.7.0
remote:        Fetching nio4r 2.1.0
remote:        Installing rack 2.0.3
remote:        Installing nio4r 2.1.0 with native extensions
remote:        Fetching websocket-extensions 0.1.2
remote:        Fetching mime-types-data 3.2016.0521
remote:        Installing websocket-extensions 0.1.2
remote:        Fetching arel 7.1.4
remote:        Installing arel 7.1.4
remote:        Installing mime-types-data 3.2016.0521
remote:        Using bundler 1.15.2
remote:        Fetching coffee-script-source 1.12.2
remote:        Fetching execjs 2.7.0
remote:        Installing coffee-script-source 1.12.2
remote:        Installing execjs 2.7.0
remote:        Fetching method_source 0.8.2
remote:        Fetching thor 0.20.0
remote:        Installing method_source 0.8.2
remote:        Installing thor 0.20.0
remote:        Fetching ffi 1.9.18
remote:        Fetching multi_json 1.12.1
remote:        Installing multi_json 1.12.1
remote:        Fetching puma 3.10.0
remote:        Installing puma 3.10.0 with native extensions
remote:        Installing ffi 1.9.18 with native extensions
remote:        Fetching rb-fsevent 0.10.2
remote:        Installing rb-fsevent 0.10.2
remote:        Fetching tilt 2.0.8
remote:        Installing tilt 2.0.8
remote:        Fetching sqlite3 1.3.13
remote:        Installing sqlite3 1.3.13 with native extensions
remote:        Fetching tzinfo 1.2.3
remote:        Installing tzinfo 1.2.3
remote:        Fetching nokogiri 1.8.0
remote:        Installing nokogiri 1.8.0 with native extensions
remote:        Fetching rack-test 0.6.3
remote:        Installing rack-test 0.6.3
remote:        Fetching sprockets 3.7.1
remote:        Installing sprockets 3.7.1
remote:        Fetching websocket-driver 0.6.5
remote:        Installing websocket-driver 0.6.5 with native extensions
remote:        Fetching mime-types 3.1
remote:        Installing mime-types 3.1
remote:        Fetching coffee-script 2.4.1
remote:        Installing coffee-script 2.4.1
remote:        Fetching uglifier 3.2.0
remote:        Installing uglifier 3.2.0
remote:        Fetching activesupport 5.0.5
remote:        Installing activesupport 5.0.5
remote:        Fetching rb-inotify 0.9.10
remote:        Installing rb-inotify 0.9.10
remote:        The latest bundler is 1.15.4, but you are currently running 1.15.2.
remote:        To update, run `gem install bundler`
remote:        Gem::Ext::BuildError: ERROR: Failed to build gem native extension.
remote:        current directory:
remote:        /tmp/build_a4bf8c817c0f6961c0c3380fee8c4817/vendor/bundle/ruby/2.3.0/gems/sqlite3-1.3.13/ext/sqlite3
remote:        /tmp/build_a4bf8c817c0f6961c0c3380fee8c4817/vendor/ruby-2.3.4/bin/ruby -r
remote:        ./siteconf20170902-218-13nbpcr.rb extconf.rb
remote:        checking for sqlite3.h... no
remote:        sqlite3.h is missing. Try 'brew install sqlite3',
remote:        'yum install sqlite-devel' or 'apt-get install libsqlite3-dev'
remote:        and check your shared library search path (the
remote:        location where your sqlite3 shared library is located).
remote:        *** extconf.rb failed ***
remote:        Could not create Makefile due to some reason, probably lack of necessary
remote:        libraries and/or headers.  Check the mkmf.log file for more details.  You may
remote:        need configuration options.
remote:        Provided configuration options:
remote:        --with-opt-dir
remote:        --without-opt-dir
remote:        --with-opt-include
remote:        --without-opt-include=${opt-dir}/include
remote:        --with-opt-lib
remote:        --without-opt-lib=${opt-dir}/lib
remote:        --with-make-prog
remote:        --without-make-prog
remote:        --srcdir=.
remote:        --curdir
remote:        --ruby=/tmp/build_a4bf8c817c0f6961c0c3380fee8c4817/vendor/ruby-2.3.4/bin/$(RUBY_BASE_NAME)
remote:        --with-sqlite3-config
remote:        --without-sqlite3-config
remote:        --with-pkg-config
remote:        --without-pkg-config
remote:        --with-sqlite3-dir
remote:        --without-sqlite3-dir
remote:        --with-sqlite3-include
remote:        --without-sqlite3-include=${sqlite3-dir}/include
remote:        --with-sqlite3-lib
remote:        --without-sqlite3-lib=${sqlite3-dir}/lib
remote:        To see why this extension failed to compile, please check the mkmf.log which can
remote:        be found here:
remote:        /tmp/build_a4bf8c817c0f6961c0c3380fee8c4817/vendor/bundle/ruby/2.3.0/extensions/x86_64-linux/2.3.0/sqlite3-1.3.13/mkmf.log
remote:        extconf failed, exit code 1
remote:        Gem files will remain installed in
remote:        /tmp/build_a4bf8c817c0f6961c0c3380fee8c4817/vendor/bundle/ruby/2.3.0/gems/sqlite3-1.3.13
remote:        for inspection.
remote:        Results logged to
remote:        /tmp/build_a4bf8c817c0f6961c0c3380fee8c4817/vendor/bundle/ruby/2.3.0/extensions/x86_64-linux/2.3.0/sqlite3-1.3.13/gem_make.out
remote:        An error occurred while installing sqlite3 (1.3.13), and Bundler cannot
remote:        continue.
remote:        Make sure that `gem install sqlite3 -v '1.3.13'` succeeds before bundling.
remote:        In Gemfile:
remote:        sqlite3
remote:        Bundler Output: Warning: the running version of Bundler (1.15.2) is older than the version that created the lockfile (1.15.4). We suggest you upgrade to the latest version of Bundler by running `gem install bundler`.
remote:        Fetching gem metadata from https://rubygems.org/.........
remote:        Fetching version metadata from https://rubygems.org/..
remote:        Fetching dependency metadata from https://rubygems.org/.
remote:        Fetching rake 12.0.0
remote:        Fetching concurrent-ruby 1.0.5
remote:        Fetching i18n 0.8.6
remote:        Installing i18n 0.8.6
remote:        Installing rake 12.0.0
remote:        Installing concurrent-ruby 1.0.5
remote:        Fetching minitest 5.10.3
remote:        Fetching thread_safe 0.3.6
remote:        Installing minitest 5.10.3
remote:        Installing thread_safe 0.3.6
remote:        Fetching builder 3.2.3
remote:        Fetching erubis 2.7.0
remote:        Installing builder 3.2.3
remote:        Fetching mini_portile2 2.2.0
remote:        Fetching rack 2.0.3
remote:        Installing mini_portile2 2.2.0
remote:        Installing erubis 2.7.0
remote:        Fetching nio4r 2.1.0
remote:        Installing rack 2.0.3
remote:        Installing nio4r 2.1.0 with native extensions
remote:        Fetching websocket-extensions 0.1.2
remote:        Fetching mime-types-data 3.2016.0521
remote:        Installing websocket-extensions 0.1.2
remote:        Fetching arel 7.1.4
remote:        Installing arel 7.1.4
remote:        Installing mime-types-data 3.2016.0521
remote:        Using bundler 1.15.2
remote:        Fetching coffee-script-source 1.12.2
remote:        Fetching execjs 2.7.0
remote:        Installing coffee-script-source 1.12.2
remote:        Installing execjs 2.7.0
remote:        Fetching method_source 0.8.2
remote:        Fetching thor 0.20.0
remote:        Installing method_source 0.8.2
remote:        Installing thor 0.20.0
remote:        Fetching ffi 1.9.18
remote:        Fetching multi_json 1.12.1
remote:        Installing multi_json 1.12.1
remote:        Fetching puma 3.10.0
remote:        Installing puma 3.10.0 with native extensions
remote:        Installing ffi 1.9.18 with native extensions
remote:        Fetching rb-fsevent 0.10.2
remote:        Installing rb-fsevent 0.10.2
remote:        Fetching tilt 2.0.8
remote:        Installing tilt 2.0.8
remote:        Fetching sqlite3 1.3.13
remote:        Installing sqlite3 1.3.13 with native extensions
remote:        Fetching tzinfo 1.2.3
remote:        Installing tzinfo 1.2.3
remote:        Fetching nokogiri 1.8.0
remote:        Installing nokogiri 1.8.0 with native extensions
remote:        Fetching rack-test 0.6.3
remote:        Installing rack-test 0.6.3
remote:        Fetching sprockets 3.7.1
remote:        Installing sprockets 3.7.1
remote:        Fetching websocket-driver 0.6.5
remote:        Installing websocket-driver 0.6.5 with native extensions
remote:        Fetching mime-types 3.1
remote:        Installing mime-types 3.1
remote:        Fetching coffee-script 2.4.1
remote:        Installing coffee-script 2.4.1
remote:        Fetching uglifier 3.2.0
remote:        Installing uglifier 3.2.0
remote:        Fetching activesupport 5.0.5
remote:        Installing activesupport 5.0.5
remote:        Fetching rb-inotify 0.9.10
remote:        Installing rb-inotify 0.9.10
remote:        The latest bundler is 1.15.4, but you are currently running 1.15.2.
remote:        To update, run `gem install bundler`
remote:        Gem::Ext::BuildError: ERROR: Failed to build gem native extension.
remote:
remote:        current directory:
remote:        /tmp/build_a4bf8c817c0f6961c0c3380fee8c4817/vendor/bundle/ruby/2.3.0/gems/sqlite3-1.3.13/ext/sqlite3
remote:        /tmp/build_a4bf8c817c0f6961c0c3380fee8c4817/vendor/ruby-2.3.4/bin/ruby -r
remote:        ./siteconf20170902-218-13nbpcr.rb extconf.rb
remote:        checking for sqlite3.h... no
remote:        sqlite3.h is missing. Try 'brew install sqlite3',
remote:        'yum install sqlite-devel' or 'apt-get install libsqlite3-dev'
remote:        and check your shared library search path (the
remote:        location where your sqlite3 shared library is located).
remote:        *** extconf.rb failed ***
remote:        Could not create Makefile due to some reason, probably lack of necessary
remote:        libraries and/or headers.  Check the mkmf.log file for more details.  You may
remote:        need configuration options.
remote:
remote:        Provided configuration options:
remote:        --with-opt-dir
remote:        --without-opt-dir
remote:        --with-opt-include
remote:        --without-opt-include=${opt-dir}/include
remote:        --with-opt-lib
remote:        --without-opt-lib=${opt-dir}/lib
remote:        --with-make-prog
remote:        --without-make-prog
remote:        --srcdir=.
remote:        --curdir
remote:        --ruby=/tmp/build_a4bf8c817c0f6961c0c3380fee8c4817/vendor/ruby-2.3.4/bin/$(RUBY_BASE_NAME)
remote:        --with-sqlite3-config
remote:        --without-sqlite3-config
remote:        --with-pkg-config
remote:        --without-pkg-config
remote:        --with-sqlite3-dir
remote:        --without-sqlite3-dir
remote:        --with-sqlite3-include
remote:        --without-sqlite3-include=${sqlite3-dir}/include
remote:        --with-sqlite3-lib
remote:        --without-sqlite3-lib=${sqlite3-dir}/lib
remote:
remote:        To see why this extension failed to compile, please check the mkmf.log which can
remote:        be found here:
remote:
remote:        /tmp/build_a4bf8c817c0f6961c0c3380fee8c4817/vendor/bundle/ruby/2.3.0/extensions/x86_64-linux/2.3.0/sqlite3-1.3.13/mkmf.log
remote:
remote:        extconf failed, exit code 1
remote:
remote:        Gem files will remain installed in
remote:        /tmp/build_a4bf8c817c0f6961c0c3380fee8c4817/vendor/bundle/ruby/2.3.0/gems/sqlite3-1.3.13
remote:        for inspection.
remote:        Results logged to
remote:        /tmp/build_a4bf8c817c0f6961c0c3380fee8c4817/vendor/bundle/ruby/2.3.0/extensions/x86_64-linux/2.3.0/sqlite3-1.3.13/gem_make.out
remote:
remote:        An error occurred while installing sqlite3 (1.3.13), and Bundler cannot
remote:        continue.
remote:        Make sure that `gem install sqlite3 -v '1.3.13'` succeeds before bundling.
remote:
remote:        In Gemfile:
remote:        sqlite3
remote:  !
remote:  !     Failed to install gems via Bundler.
remote:  !     Detected sqlite3 gem which is not supported on Heroku:
remote:  !     https://devcenter.heroku.com/articles/sqlite3
remote:  !
remote:  !     Push rejected, failed to compile Ruby app.
remote:
remote:  !     Push failed
remote: Verifying deploy...
remote:
remote: !   Push rejected to still-inlet-93643.
remote:
To https://git.heroku.com/still-inlet-93643.git
 ! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to 'https://git.heroku.com/still-inlet-93643.git'

解決方法

$ bundle install --without production
Bundle complete! 15 Gemfile dependencies, 61 gems now installed.
Gems in the group production were not installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.

??なのだが、これやった後にheroku pushしたらでけた。

これやった前後の違いは、こいつが新たに出来上がった

# .bundle/config
---
BUNDLE_WITHOUT: "production"

これによってなんかきちんと設定を見るようになったんだと思う。

結構なはまりポイントだと思うので、何かお役に立てれば幸いです。

Googleフォームで先着順=回答数を制限する!を10分で実現する

Googleフォームのアドオン「formLimiter」の画像
Googleフォームのアドオン「formLimiter」の画像

アンケートを行いたい時に便利なのが「Googleフォーム」。

簡単にアンケート機能が実現できるし、集計も簡単にできるので大変便利なのですが

先着10名様限定!

みたいな回答数に上限を持たせるような機能を実現させたい時があると思います。

今回は、その方法をたった10分で実現する方法を紹介します。

ざっくり手順

  1. Googleフォーム作成
  2. 画面右上の「:」をクリック
  3. 「アドオン」をクリック
  4. 「formLimiterをインストール
  5. 「formLimiter」の設定を行う

アドオン「formLimiter」の使い方

具体的な設定方法は以下の手順で行えばok!

アドオン「formLimiter」の設定画面の画像
アドオン「formLimiter」の設定画面の画像

  • Limit Type:number of form responses -> 回答数で制限を行う
  • when responses are greater than:入力した数で回答を締め切る
  • Message when submissions are closed:締め切られた後に表示する文言
  • Email from owner when submissions are closed:締め切ったタイミングでメールを送信するかどうか

回答数の制限以外にも

  • 締め切り期限 = 終了する日時の設定
  • スプレッドシートの値に応じた制御

など結構柔軟な対応が、これだけで実現可能です!

GASを使う

formLimiterを使えば簡単に出来るが、スクリプトを自分で書くことで同様なことが実現可能です。

function myFunction() {
  // 回答上限数
  LIMIT_COUNT = 10;

  // アクティブフォーム取得  
  form = FormApp.getActiveForm();
  // 回答数を取得
  responseCount = form.getResponses().length;
  
  // 回答数を上回ったらアンケートを回答できなくする
  if( responseCount >= LIMIT_COUNT ) {
    form.setAcceptingResponses(false);
  }
  
  // 回答数をログに出力する
  Logger.log("回答数: " + responseCount);
}

各コードでやっていることは以下です。

function hoge()

関数定義。これが実行されるプログラムの一つの塊になります。

form = FormApp.getActiveForm()

Googleフォームからスクリプトエディタを起動させてGASを作成するとアクティブなGoogleフォームという形で紐付き簡単に呼び出せる。

form.getResponses()

回答された全てのデータを取得する

form.getResponses().length

回答された全てのデータ件数を取得する

if ( responseCount >= LIMIT_COUNT)

if文は条件分岐をさせる。今回の場合は回答上限数を回答数が上回ったら{}で囲まれた中に処理が通る。

form.setAcceptingResponses(false)

アンケート受け付けを制御する。falseの場合は受付をストップさせる。

Logger.log(responseCount)

()の中にあるデータをログに出力する

まとめ

10分もあれば、先着10名様!機能をGoogleフォームで実現できたと思います。

アドオンも結構色々なものがあったので、便利そうですね。

TwitterやFacebookなどのSNSボタンを5分で追加する

Railsアプリを作る時、SNSのシェアボタンを設置するにはどうすれば良いのでしょうか?

Rails social-share-button gemのイメージ画像
Rails social-share-button gemのイメージ画像

今回たまたまsocial-share-buttonというgemを見つけたので、それを使ってみました。 https://github.com/huacnlee/social-share-button

やり方はREADME.mdに書かれていますが、まとめます。

ざっくり手順紹介

ざっくり手順紹介

  1. Gemfileに追加してbundle installする
  2. application.cssに追加する
  3. application.jsに追加する
  4. viewにsocial_share_button_tagを設定する

Gemfileの設定

# Gemfile
gem 'social-share-button'

コマンドはお馴染みのbundle install

css設定

# application.css
#= require social-share-button

js設定

# application.js
//= require social-share-button

viewの設定

# show.html.haml
%p#notice= notice
%h2
  = @event.title
= social_share_button_tag(@event.hash_tag, :allow_sites => %w(twitter facebook google_plus weibo qq douban google_bookmark delicious tumblr pinterest email linkedin wechat vkontakte xing reddit hacker_news telegram odnoklassniki))

これだけ!!

第一引数に渡した文字列がツイートの本文になる。

twitterのボタンを押した時の画像
twitterのボタンを押した時の画像

とりあえず全部アイコン出してますけど、半分以上分からない...

あと、facebookは使えないんだって。 Error_と_OhkEventRails.png

まーこんな感じです。

README.md通りやると途中でrails gコメンドでファイル作成して、そっちに表示したいアイコンの配列を作るのですがエラーになって上手くいかなかった。 多分上手くそのファイルが読み込めてないのだけど、原因わからないからviewから直接渡しちゃってる。

とりあえず出来たからOK!

css上で動的に値の変更はできないけどhtmlに書けばできる

cssに設定する値を動的に変更するとかできないのか?なんて思ったことないでしょうか?

例えば、

  • ユーザー毎にボタンの色や背景色などを変更したい!
  • 条件に応じてbackgroundに画像を入れたり、差し替えたりしたい!

結論から言うとできます!

ただcssでは出来ないのでhtmlにstyle属性を指定してcssを書き込めばokです!

今回はナビゲーションバーにある予約ボタンをユーザー毎に変更する例です。

model

前提としてユーザー = userモデルがあり、そこにカラーコードが設定されているとします。

id name color created_at updated_at
1 opiyo 30a8b5
2 kuma 000
3 fuzo fff

シンプルな使い方

view

haml

.event__nav
  = link_to '予約', '#', style: "background-color: ##{user.color}"

html

<div class="event__nav">
  <a style="background-color: #30a8b5" href="#">予約</a>
</div>

条件がいっぱいあるときの使い方

view

haml

.event__nav
  = link_to '予約', '#', style: event_nav_btn_style(user)"

link_toは第三引数にstyle属性を使えば、htmlに直接cssの設定ができます。

html

<div class="event__nav">
  <a style="background-color: #30a8b5" href="#">予約</a>
</div>

helper

条件が多かったりする場合はhtmlからhelperメソッドを呼び出して上げればokです!

def event_nav_btn_style(user)
  if user.name == 'hogehogehoge'
    return 'background-color: #FF7A00;'
  elsif user.name == 'piyopiyopiyo'
    return "background-color: ##{user.color};"
  else
    return 'background-color: black;'
  end
end

ActiveJobを使った非同期処理の方法

Ruby on Railsを使って非同期で処理する方法 ActiveJobについてです。

ざっくり手順

  1. generateでjobファイルを作成する
  2. jobをキューに登録する
  3. jobを実行する

generateでjobファイルを作成する

コマンドで $ bundle exec rails g job hoge_job

手作業で jobs/hoge_job.rbに.rbファイルを作成する

jobをキューに登録する

HogeJob.perform_later(event_id: @event.id)

perform_later: 実行キューが積まれて随時処理する

perform_now: 今すぐに実行する

時間指定する方法

明日の正午("2019/06/12 12:00")

HogeJob.set(wait_util: Date.tomorrow.noon).perform_later()

1週間後

HogeJob.set(wait: 1.week).perform_later()

※要注意は時間を指定してはいけない。あくまで日数を指定すること。

呼び出された方

#jobs/hoge_job.rb
class HogeJob
  queue_as :default

  URL = Rails.application.secrets.url
  SUCCESS = 200
  FAILED = 400

  def perform(event_id, status_code = 100)
    response = RestClient.get "#{URL}?event_id=#{event_id}&status_code=#{status_code}"
    result = JSON.parse(response.body).dig('root')
    if result['code'] == FAILED
      Bugsnag.notify(exception, { event_id: event_id, error_message: result['message']})
    end
  end
end

コールバックを指定することもできるので、model的な感じで色々できそうです。

参考サイト

Active Job の基礎 - Rails ガイド

Ruby on Railsでcsvダウンロード機能の作り方

Ruby on Railsを使ってcsvファイルのダウンロード機能を作りたくて調べました。

大枠の手順

  1. routesを設定
  2. csvを受け付けるアクションをコントローラーに設定
  3. 対象データを抽出しcsvフォーマットのファイルへ渡す
  4. CSVデータを作成する
  5. 画面にダウンロードボタンを作成する

コントローラーにアクションを定義する

# controller/users_controller.rb
  def index
    @users = User.all

    respond_to do |format|
      format.html
      format.csv do
        filename = ERB::Util.url_encode('ユーザー一覧.csv')
        send_data render_to_string, filename: filename, type: :csv
      end
    end
  end

ERB::Util.url_encodeは引数に渡された文字列をURLエンコードしてくれる。これによって日本語や半角スペースをよしなにしてくれる

send_dataは動的に生成されたデータをダウンロードする。 render_to_stringは表示結果を文字列として取得するメソッド。

なので、これらを組み合わせる事でCSVファイルに出力する為の文字列を作ってダウンロードを可能にしている。

参考: https://docs.ruby-lang.org/ja/latest/class/ERB=3a=3aUtil.html

参考: http://railsdoc.com/references/send_data

参考: http://railsdoc.com/references/render_to_string

View

# views/users/index.haml
= link_to 'CSVダウンロード' users_path(format: :csv)

ポイントはリンクを生成するpathにフォーマット: csvを指定してあげます。これによってコントローラー側で切り分けれるようになります。

csvファイル

# views/users/index.csv.ruby
require 'csv'
require 'nkf'
csv_data = CSV.generate do |csv|
  csv << %w(id 名前 年齢)
  @users.each do |user|
    csv << [
        user.id,
        user.name,
        user.age
    ]
  end
end
NKF::nkf('--sjis -Lw', csv_data)

CSV.generateは文字列csv形式=カンマ区切りの文字列を生成してくれます。 NKF::nkfは文字コードを強制的に変換する為のプログラムです。

参考: https://docs.ruby-lang.org/ja/1.9.3/class/NKF.html

【Rails】Strong Parametersで`param is missing...`エラーになる

こんちには。opiyoです。

ActionController::ParameterMissing in ImagesController#create
param is missing or the value is empty: image

    def image_paprams
      params.require(:image).permit(:name, :picture)
    end

end

こんな感じでエラーになるのだが、paramsが空っぽだよと。 実際にparams[:image]ってやるとnilが返ってくる。

が、すげー凡ミスでpermitて定義している:nameをview側に書いてなかった。 だから、来るはずのデータが来てなくてエラーになったのではなかろうかと思ってる。

<%= form_for(@image) do |f| %>
  <div class="form-group">
    <%= f.text_field :name %> # ここを書いてなかったけど、書いたらエラーにならなくなった    
    <%= f.file_field :picture %>
    <%= f.submit "画像を投稿する" %>
  </div>
<% end %>

そんな感じ。

Docker For MacでPostgreSQL9.5系の環境を作ってみた!

こんばんは。opiyoです。

brewでインストールするとなんだかPostgreSQL9.6系がインストールされてしまうのだけど、本番環境と同じバージョンをローカルに使いたくてウズウズしていたのだが...

Docker使えば良いじゃんってアドバイス貰ったので教えてもらった

Docker For Macのインストール

https://www.docker.com/docker-mac

docker-compose.ymlを作る

docker-compose.ymlの場所を作る

$ mkdir -p ~/compose/postgresql95/
$ vi docker-compose.yml

PostgreSQL9.5を配置する場所を作る

$ mkdir ~/postgres95

docker-compose.ymlを修正

postgresql:
  image: postgres:9.5.8-alpine
  environment:
    POSTGRES_USER: taku
    POSTGRES_INITDB_ARGS: "--encoding=UTF8 --no-locale"
    LANG: ja_JP.UTF-8
  ports:
    - "5432:5432"
  volumes:
    - /Users/taku/postgres95:/var/lib/postgresql/data #「/Users/taku/postgres95」の箇所を、さっき作った場所を指定する      
  restart: always

やることは、

  • POSTGRES_USERを各自設定する
  • volumesのパスをさっき作った場所を指定する

imageの部分には、こちらのサイトに書いてあるように好きなバージョンを書けばおk https://hub.docker.com/r/library/postgres/tags/

自動起動されているpostgresqlを止める

本当は9.6系と9.5系を共存させたいのだけど、上手くいかないので既にPostgreが動いている場合は止める。 私の場合は、brewでインストールしてたので簡単だった。

$ brew services list
Name       Status  User
postgresql started taku

$ brew services stop postgresql

$ brew services list
Name       Status  User
postgresql stopped

パスを通す

$ sudo vi ~/.bash_profile
export PGHOST=localhost

$ env | grep HOST
PGHOST=localhost

これは教えてもらうがままにやったけど、何してるのかいまいち分かってないぞ。

構築と起動

$ docker-compose up -d

psql -l -p5432とかやればアクセスできるはず! めちゃくちゃ簡単でした。

その他Dockerコマンド

ステータス

$ docker ps

停止

$ docker stop CONTAINER_NAMES

コンテナに入る

$ docker exec -it CONTAINER_NAMES /bin/bash

こんな感じ。

【Rails】ファイルのフルパス、ファイル名を取得する

過去データとかでファイルを一括で読み込みたい場合で使える技です。

> files = Dir.glob("/Users/taku/rails/gist/test/*.xls")
=> ["/Users/taku/rails/gist/test/1.xls", "/Users/taku/rails/gist/test/2.xls", "/Users/taku/rails/gist/test/3.xls", "/Users/taku/rails/gist/test/4.xls", "/Users/taku/rails/gist/test/5.xls"]

これでfilesをグルグル回して一つ一つのファイルを読み込んでやれば色々処理できますね。 取り込み処理も出来るし、csvを作ったりも出来るし。

globを使うと拡張子で絞り込めるので、結構便利ですね。

> files = Dir.entries("/Users/taku/rails/gist/test/")
=> [".", "..", ".DS_Store", "1.csv", "1.txt", "1.xls", "1.xlsx", "2.csv", "2.txt", "2.xls", "2.xlsx", "3.csv", "3.txt", "3.xls", "3.xlsx", "4.xls", "5.xls"]

単純にディレクトリの中にあるファイル名だけ取得したい場合はentriesを使うそうです。

【Ruby】配列の中身が重複しているかをチェックする方法(select、find)

csvのデータを取り込み別のcsvへ吐き出す処理をしていたのですが、値が重複していることに気がつきました。

こんな感じ。

data = []
inport = CSV.read("./inport.csv")
inport.each do |c|
  data << c
end

CSV.open("./export.csv", "w") do |export|
  data.each do |d|
    export << d
  end
end

この中で取り込んだデータが重複しているかどうかチェックする方法が分からず、グルグル回さないとダメなのかと思っていたのですが良い方法がありました。

> ff6 = [["Tina", 1],["Rokku", 2],["Edoga", 3],["Masshu", 4]]
=> [["Tina", 1], ["Rokku", 2], ["Edoga", 3], ["Masshu", 4]]

> ff6.select{|f| f[0] == "Masshu"}
=> [["Masshu", 4]]
irb(main):003:0>

selectを使うと合致したものだけを取ってきてくれます。 なので重複しているかチェックしたい場合は、sizeを使って数を調べてやれば良いです。

> ff6.select{|f| f[0] == "Masshu"}.size
=> 1

> if ff6.select{|f| f[0] == "Masshu"}.size > 0
>   # 処理
> end

めでたし、めでたし

追記

findを使った方が早いとのこと。

> ff6
=> [["Tina", 1], ["Rokku", 2], ["Edoga", 3], ["Masshu", 4]]

> ff6.find {|ff| ff[0] == "Masshu" }
=> ["Masshu", 4]

> ff6.find {|ff| ff[0] == "Masshu" }.size
=> 2

selectとの違いは、見つけた最初の配列を一つだけ返す。ってことですかね。 例えば、

# 「カイエン 4」を追加します
> ff6 = [["Tina", 1],["Rokku", 2],["Edoga", 3],["Masshu", 4], ["Kaien", 4]]
=> [["Tina", 1], ["Rokku", 2], ["Edoga", 3], ["Masshu", 4], ["Kaien", 4]]


# selectの場合
> ff6.select {|ff| ff[1] == 4 }
=> [["Masshu", 4], ["Kaien", 4]]

# findの場合
> ff6.find {|ff| ff[1] == 4 }
=> ["Masshu", 4]

最初に見つけた一つを返すんだから、そりゃー早いは。 今回やりたいことは、重複しているかのチェックだから1件でもあればNG。 ってことでfindを使うことにした。