ラベル rails の投稿を表示しています。 すべての投稿を表示
ラベル rails の投稿を表示しています。 すべての投稿を表示

2015年6月29日月曜日

kaminari のpagination (view) を javascript で表示する (ajax)



ページネーションに kaminari を使っている場合。
一部だけ ajax 経由で受け取ったjson データを表示しなければならない場合、どうしたらいいのか?

これが一番簡単だと思う。
ajax に応答するコントローラー内で
 -  view_context.paginate() を呼び出して、pagination のHTMLを作成
 -  作成したHTMLを json に埋め込んで返す
 - view内のjs は受け取ったjson 内のHTML を表示

http://pistachio0416.hatenablog.com/entry/2015/03/12/Kaminari%E3%82%92json%E3%81%A7ajax%E5%8C%96%E3%81%99%E3%82%8B

TODO:
kaminari にjavascript での表示コードが含まれているかもしれない。
(それか、GitHub で探す)

2015年4月7日火曜日

link_to のラッパー


link_to の拡張

# app/helpers/application_helper.rb
def link_to(text, path, options = {}, &block)
  # do someting
  super
  # do someting
end


To extend rails' `link_to`, should I use `alias_method_chain` or mixins + inheritance?
http://stackoverflow.com/a/13566131

オリジナルのlink_to はactionview の
lib/action_view/helpers/url_helper.rb
に定義されてる。

2015年2月25日水曜日

turbolinks で jquery.ready が動かない


var ready = function() {
  ...your javascript goes here...
};

$(document).ready(ready);
$(document).on('page:load', ready);  // turbolinks対応


http://stackoverflow.com/questions/18770517/rails-4-how-to-use-document-ready-with-turbo-links


page:load, ready 以外にも
https://github.com/rails/turbolinks/

2015年2月21日土曜日

Passenger 5


passenger が刷新されてる?

https://www.phusionpassenger.com/
https://github.com/phusion/passenger


Passenger 5 (コードネームはラプター)

「RaptorはどのようにしてUnicornの4倍、Puma, Torqueboxの2倍の速度を達成したのか」を読んでまとめてみた


とりあえず、standalone で動かしてみた。
(nginx は yum でインストールしたものなので、standalone しか道はない)
環境は centos7, rbenv
nginx --- passenger5 --- rails


インストール
rails のGemfile に追加# Gemfile
gem passenger, '5.0.5'

$ bundle install --path vendor/bundle

# rails のbin にpassenger コマンドを追加
# bin/passenger が作成される
bundle install --binstubs
(bin/passenger でなく、bundle exec passenger でもたぶんOKだと思う)

# 確認
$ bin/passenger start
 (デフォルトでは http://0.0.0.0:3000/ )

# nginx 経由でアクセスする。
# ドメインは raptor.local として、hosts に登録しておく。

$ vi /etc/nginx/conf.d/raptor.local.conf
server {
    listen 80;
    server_name raptor.local;

    location / {
        proxy_pass http://0.0.0.0:3000;
    }
}

# 変更を反映させる
$ systemctl reload nginx

これで、http://raptor.local/ でアクセスできる。
次に、systemd に登録してデーモン化する。

# サービス名を raptor.service としておく。
vi /etc/systemd/system/raptor.service

[Unit]
Description=Raptor (passenger5 standalone) with rails
After=syslog.target network.target nss-lookup.target

[Service]
Type=forking
User=hoge
Group=hoge
WorkingDirectory=/public/raptor
PIDFile=/public/raptor/tmp/pids/passenger.3000.pid

ExecStart=/public/raptor/bin/passenger start --daemonize
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

systemctl enable raptor
systemctl start raptor

# /etc/systemd/system/raptor.service を変更したら
$ systemctl daemon-reload


メモ、注意点、TODO
 - bin/passenger start --help でオプションの確認ができる
 -  --deamonize は必須
 - Unix domain の利用を考える (--socket オプション)
 - プロセス数の指定
 - この設定だと development でrails が動いているので変更も即座に反映してくれる。
(production 指定も調べる)



最後にrbenv について
rbenv はユーザーhoge だけに有効なので
(/home/hoge/.rbenv にインストールした)
bin/passenger の先頭を以下のように変更した。
#!/usr/bin/env ruby
  ↓
#!/home/hoge/.rbenv/versions/2.2.1/bin/ruby

/usr/local にインストールしたり、他の方法もあるのかもしれないけど、わからないので、自分(作業ユーザー) のrbenv を直接指定して動かした。

2015年2月10日火曜日

rubocop のcopname


エラーを表示しているcop name を表示する方法

rubocop -D (D オプション)
これで、cop name も表示されるので、rubocop disable 時に迷わない

rubocop はoffence 毎にcop がいるということなのかな?

2015年2月9日月曜日

rails プロジェクトの作成メモ


環境 rails4.2

systemにrails を入れない場合
Rspec, Postgres を利用する。

プロジェクト名が projx の場合
$ mkdir projx
$ cd projx

# vender/bundle にrails を入れるため、仮のGemファイル作成
# ENV['NOKOGIRI_USE_SYSTEM_LIBRARIES'] はnokogiri対策
source 'http://rubygems.org'

ENV['NOKOGIRI_USE_SYSTEM_LIBRARIES'] = 'YES'
gem 'rails'

# rails を入れる
$ bundle install --path vendor/bundle

$ bundle exec rails new . -T -d postgresql --skip-bundle --skip-sprockets
-T : テスト作成スキップ (Rspec を使う)
-d : デフォルトは sqlite (mysql, postgresql)
(rails new --help で確認)

# postgres を使う場合
# bundle installの前に以下を実行。
bundle config build.pg --with-pg-config=/usr/pgsql-9.4/bin/pg_config
(.bundle/config へ追加しておくほうがいいかも)

# Gemfile を編集
ENV['NOKOGIRI_USE_SYSTEM_LIBRARIES'] = 'YES' 追加 (先頭)
gem 'rspec-rails'  追加
gem 'therubyracer'  追加
gem 'coffee-rails'  削除

# gemは全てプロジェクト内に入れる
bundle install --path vendor/bundle

# Rspec 初期化
bundle exec rails g rspec:install

# spring の用意
bundle exec spring binstub --all

2014年10月23日木曜日

rails のSECRET_KEY_BASE でエラー


railsをproductionモードで起動したらエラー

Internal Server Error 
Missing `secret_key_base` for 'production' environment, set this value in `config/secrets.yml`

config/secrets.yml
productionモード以外は固定値が入っている。
rails4.1から?

とりあえずこれで動く。
# export SECRET_KEY_BASE=`bundle exec rake secret`
# rails s -e production

実際の運用ではどう設定しようか?

2014年8月31日日曜日

2014年8月24日日曜日

mongodb 関連

mongoコマンド (クライアント)

サーバー
mongod
confファイル

RDB  データベース → テーブル → レコード
mongodb データベース → コレクション → ドキュメント

ドキュメント
http://docs.mongodb.org/manual/

■ 説明リンクなど
mongoコマンドまとめ
MongoDB University
MongoDBでゆるふわDB体験
BSON

■ rails での利用 Mongoid (pronounced mann-goyd)
 参考リンク How to Host Ruby on Rails 4 Apps on OpenShift

# rails new proj01 -O  # -O:active recordをスキップ、 --skip-active-record でも同じ
# rails g mongoid:config

マイグレーション用ファイルは必要ない。modelファイルに直接定義。

問題点
ver4から DateTime fieldsがサポートされなくなった。
mongoid-sadstory で対応?

■ 参考
MongoDB vs MySQL性能比較 (2013/11)

2014年7月7日月曜日

railsのルート名に "_index"がつく場合

resource名が単数系の場合に suffix "_index"が引っ付く。
単複同形の名詞も同じ。

# vi config/routes.rb
namespace :admin do
     resources :companies
     resources :series
end

# rake routes
admin_companies GET    /admin/companies(.:format)          admin/companies#index
admin_series_index GET    /admin/series(.:format)             admin/series#index

単複同形は罠だった。
sheepとかも。 遠い昔勉に強した気もする。。。

このルールを変更するには。
config/initializers/inflections.rb

英単語
plural:(形容詞) 複数の、複数形  (反対:singular)
単複同形:the same plural and singular form

2014年2月11日火曜日

rails の実行モード


■ コード内でモードを取得
Rails - How do I check developer mode or production mode in code
http://stackoverflow.com/questions/6476832/rails-how-do-i-check-developer-mode-or-production-mode-in-code

Rails.env == "production"
Rails.env == "development"
Rails.env.production?
Rails.env.development?

■ 実行時にオプションで指定する
rake db:migrate RAILS_ENV=test
rails s -e production

2014年1月24日金曜日

rails マイグレーションのやり直し



1つだけ
rake db:migrate:down VERSION=xxxxxxx
rake db:migrate:up VERSION=xxxxxxx
rake db:migrate:redo VERSION=xxxxxxx

全て
rake db:migrate:reset

 rubyで実行
http://stackoverflow.com/a/1417212