個人的なメモ書きです。
セットアップ
Xcodeのセットアップ
- Mac App Storeからインストールする
- インストール後、いちど起動して、利用規約に同意する
- これをやっておかないと後でエラーが出る
次に、
xcode-select --install
Homebrewのセットアップ
$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
- http://brew.sh/ の手順通り
Rubyのセットアップ
$ brew update
$ brew install ruby
PostgreSQLのインストール
$ brew install postgresql
Railsプロジェクトの新規作成
-
fuga
という名前のプロジェクトを作成 - 自動で行われる
bundle install
をスキップする(後ほどGemfileを編集するから二度手間になる) - DBにPostgreSQLを使用する
$ rails new fuga --skip-bundle -d postgresql
$ cd fuga
$ bundle install --path vendor/bundle --without production
DBの作成, postgisの導入などを簡単に済ませたい
簡単というかこれがrailsでやるときの標準的なやりかたなのかな。
知らずに手動でDBつくったりしてた・・・
bundle exec rake -T db
でdbに関するrakeタスクの一覧が表示できる
$ bundle exec rake -T db
rake db:create # Creates the database from DATABASE_URL or config/database.yml for the current RAILS_ENV (use db:create:all to crea...
rake db:drop # Drops the database from DATABASE_URL or config/database.yml for the current RAILS_ENV (use db:drop:all to drop all...
rake db:fixtures:load # Load fixtures into the current environment's database
rake db:gis:setup # Setup PostGIS data in the database
rake db:migrate # Migrate the database (options: VERSION=x, VERBOSE=false, SCOPE=blog)
rake db:migrate:status # Display status of migrations
rake db:rollback # Rolls the schema back to the previous version (specify steps w/ STEP=n)
rake db:schema:cache:clear # Clear a db/schema_cache.dump file
rake db:schema:cache:dump # Create a db/schema_cache.dump file
rake db:schema:dump # Create a db/schema.rb file that is portable against any DB supported by AR
rake db:schema:load # Load a schema.rb file into the database
rake db:seed # Load the seed data from db/seeds.rb
rake db:setup # Create the database, load the schema, and initialize with the seed data (use db:reset to also drop the database fi...
rake db:structure:dump # Dump the database structure to db/structure.sql
rake db:structure:load # Recreate the databases from the structure.sql file
rake db:version # Retrieves the current schema version number
rake test:all:db # Run tests quickly, but also reset db
rake test:db # Run tests quickly, but also reset db
dbの作成
rake db:create
デバッグ
byebug
を使うようになったぽい。
Simply drop
byebug
wherever you want to start debugging and the execution will stop there.
とのこと。
開発環境
ローカルでrdocを読みたい
rdoc server
open http://[::]:8808/ # せっかくなのでIPv6で
bundle exec
やめたい
普通にrails s
とかするときにもうそんなもの必要ないよ
ローカルで実行中のrails server
を一時的に外部に公開したい
ngrokを使うのがよい。
Generator
アクションを指定してControllerを追加したい
$ rails g controller devices action1 action2
既存のテーブルにカラムを追加したい
-
devices
テーブルにtext
型のssh_public_key
カラムを追加する
rails g migration add_ssh_public_key_to_devices ssh_public_key:text
scaffoldでリレーションをつくりたい
$ rails g scaffold Post
$ rails g scaffold Comment post:references
ネストされたコントローラを生成する
rails g controller admin/users
Controller
リクエストヘッダを取得する
curl -v -H "ACCESS_KEY: 82b8e91f047be83c252009d08c3def88" http://localhost:3000/api/devices/980190967.json
というリクエストの場合、
request.headers[:HTTP_ACCESS_KEY]
で取得できる。
レスポンスをJSONに限定する
def index
@users = User.all
render "index", :formats => [:json], :handlers => [:jbuilder]
end
Test::Unit
テストAPIのドキュメント
その他
DBをつくりなおす
db/seeds.rb
を書いておく。
rake db:drop
rake db:create
rake db:migrate
rake db:seed
rake test
でフルスタックトレースを表示する
config/initializers/backtrace_silencers.rb
を編集して、次の行のコメントをはずす。
-#Rails.backtrace_cleaner.remove_silencers!
+Rails.backtrace_cleaner.remove_silencers!
任意のURLを組み立てる
# httpからはじまるURLの場合
URI::HTTP.build(:host => "www.google.com", :query => { :q => "test" }.to_query)
# pathだけの場合
URI::Generic.build(:path => '/auth/github', :query => { :origin => new_app_path }.to_query)
devise gemを使うときの注意
test helperを追加しておかないと、functional testでNoMethodError: undefined method
id' for nil:NilClass`というエラーが出る。
http://stackoverflow.com/questions/31657430/nomethoderror-undefined-method-user-for-nilnilclass
https://github.com/plataformatec/devise#test-helpers
rails runner でURLヘルパーを使う
$ rails runner "helper = ApplicationController.helpers; helper.class_eval { include Rails.application.routes.url_helpers }; p helper.quotes_path"
"/quotes"
参考: https://coderwall.com/p/s5sjqq/accessing-view-helpers-in-a-rails-runner-session
資料
-
Rails Webook
- 日本語のとても豊富な情報