Category Archives: rails

docker rails batch

Dockerfile

FROM ruby:2.3.3
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs
RUN mkdir /myapp
WORKDIR /myapp
ADD Gemfile /myapp/Gemfile
ADD Gemfile.lock /myapp/Gemfile.lock
RUN bundle install
ADD . /myapp

docker-compose.yml

version: '2'
services:
  db:
    image: mysql
    ports:
      - "3306:3306"
    environment:
      MYSQL_ALLOW_EMPTY_PASSWORD: "yes"
  web:
    build: .
    # command: bundle exec rails s -p 3000 -b '0.0.0.0'
    volumes:
      - .:/myapp
    ports:
      - "3000:3000"
    depends_on:
      - db

Gemfile

source 'https://rubygems.org'
gem 'rails', '4.2.6'

Gemfile.lock

ブランク

railsアプリ作成

docker-compose run web rails new . --force --database=mysql --skip-bundle

db設定変更

version: '2'
services:
  db:
    image: mysql
    environment:
      MYSQL_ALLOW_EMPTY_PASSWORD: "yes"
  web:
    build: .
    command: bundle exec rails s -p 3000 -b '0.0.0.0'
    volumes:
      - .:/myapp
    ports:
      - "3000:3000"
    depends_on:
      - db

image作成

docker-compose build

実行

docker-compose run web rake db:create
docker-compose run web rails c

rails c 日本が???になるケースの対応

原因

localeのエラー

対応

// エラーがでていることを確認
locale 

// 設定変更
export LC_ALL=ja_JP.utf8

// エラーが解消されていることを確認
locale

// ruby, railsインストール
rbenv install 2.2.4
rbenv global 2.2.4
gem install rails

参考

http://stackoverflow.com/questions/35621614/i-got-warning-setlocale-lc-ctype-when-i-ssh-connect-to-vagrant-centos-6

radio button form_tag

.select
  = label 'validation_setting', "ON", class: 'radio-inline' do
    = radio_button_tag 'validation_setting', "ON", {:checked => true}
    %span ON
  = label 'validation_setting', "OFF", class: 'radio-inline' do
    = radio_button_tag 'validation_setting', "OFF"
    %span OFF

select2 ajax

gem

gem "select2-rails"
gem 'ransack'

html

= form_for @content, url: club_contents_path(params[:club_access_token]) do |f|
  .form-group
    = f.label '部長'
    .select
      - if params["leader_employee_id"]
        = select_tag 'leader_employee_id', options_for_select(@employees.collect{ |u| [u.profile, u.id] }, selected: params["leader_employee_id"]), { class: 'chosen-select form-control', multiple: false, prompt: "名前か従業員番号を入力して、検索してください" }

js

contents.coffee

$ ->
  $('.chosen-select').select2({
    placeholder: "名前か従業員番号を入力して、検索してください"
    ajax:
      url: '/employees/search.json'
      dataType: 'json'
      delay: 250
      cache: false
      data: (params) ->
        q: params.term
      processResults: (data, params) ->
        results: $.map data, (obj)->
          id: obj.id
          text: obj.name
  })

root

get 'employees/search' => 'employees#search'

コントローラー

class EmployeesController < ApplicationController
  def search
    q = params[:q]
    if q.blank?
      @employees = nil
    else
      @employees = Employee.search(:name_cont=>q).result
    end
    render json: @employees
  end
end

参考

https://select2.github.io/examples.html
http://blog.scimpr.com/2016/01/04/rails4-2%E3%81%A7select%E3%82%92%E7%B5%9E%E8%BE%BC%E3%81%A7%E3%81%8D%E3%82%8Bselect2%E3%82%92%E4%BD%BF%E3%81%A3%E3%81%A6%E3%81%BF%E3%81%9F%E3%80%9Cselect2/

rails routeのパスにidを使う

テーブル

t.string :access_token, :null => false

コントローラー

after_initialize :set_access_token

private
def set_access_token
  self.access_token = self.access_token.blank? ? generate_access_token : self.access_token
end                                                                                                                                                                                          

def generate_access_token
  tmp_token = SecureRandom.urlsafe_base64(6)
  self.class.where(:access_token => tmp_token).blank? ? tmp_token : generate_access_token
end

ルート設定

resources :schedules, param: :access_token

参考

http://qiita.com/ainame/items/eb9dcf7862630c44d5e9
http://qiita.com/mudai/items/34501ddd2f0bd834f303