読者です 読者をやめる 読者になる 読者になる

nirasan's tech blog

趣味や仕事の覚え書きです。Linux, Perl, PHP, Ruby, Javascript, Android, Cocos2d-x, Unity などに興味があります。

Rails初期化チートシート

プロジェクトの作成

rails new PROJECT_NAME -T -B
cd PROJECT_NAME

git の設定

git init
curl https://www.gitignore.io/api/rails,ruby,osx,linux,vim,sublimetext,rubymine > .gitignore

gemのインストール

cp /path/to/Gemfile ./ #TODO
bundle install

bootstrap の適用

rails g layout:install bootstrap3

font-awesome の使用

vi app/assets/stylesheets/framework_and_overrides.css.scss
 @import "bootstrap-sprockets";
 @import "bootstrap";
+@import "font-awesome";

simple_form

rails g simple_form:install --bootstrap

devise

rails g devise:install
rails g devise user
rake db:migrate

devise の日本語対応

rails g devise:views:locale ja
rails g devise:views:bootstrap_templates

日本語化

curl -L https://raw.github.com/svenfuchs/rails-i18n/master/rails/locale/ja.yml > config/locales/ja.yml

アプリケーション設定

vi config/application.rb
config.time_zone = 'Tokyo'
config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
config.i18n.default_locale = :ja
	
config.generators do |g|
  g.javascripts false
  g.stylesheets false
  g.helper false
  g.test_framework :rspec,
  fixture: true,
  view_specs: false,
  helper_specs: false,
  routing_specs: false,
  controller_specs: true,
  request_specs: true
  g.fixture_replacement :factory_girl, dir: 'spec/factories'
end

devise 用のリンクを app/views/layouts/_navigation_links.html.haml に追加

- if user_signed_in?
  %li= link_to current_user.email, root_path, :class => 'navbar-link'
  %li= link_to 'Edit profile', edit_user_registration_path, :class => 'navbar-link'
  %li= link_to "Logout", destroy_user_session_path, method: :delete, :class => 'navbar-link'
- else
  %li= link_to "Sign up", new_user_registration_path, :class => 'navbar-link'
  %li= link_to "Login", new_user_session_path, :class => 'navbar-link'

kaminari

rails g kaminari:config
rails g kaminari:views  bootstrap3

rspec

rails g rspec:install

テストの設定

spec/rails_helper.rb

RSpec.configure do |config|
  config.before :suite do
    DatabaseRewinder.clean_all
  end

  config.after :each do
    DatabaseRewinder.clean
  end
  
  config.before :all do
    FactoryGirl.reload
  end
end

spec/spec_helper.rb

  require 'factory_girl_rails'
  config.include FactoryGirl::Syntax::Methods

spec/factories/users.rb

actoryGirl.define do
  factory :user do
    sequence(:email) { |i| "user#{i}@example.com" }
    sequence(:password) { |i| "password#{i}" }
  end

end

.pryrc の作成

begin
  require 'hirb'
rescue LoadError
  # Missing goodies, bummer
end

if defined? Hirb
  # Slightly dirty hack to fully support in-session Hirb.disable/enable toggling
  Hirb::View.instance_eval do
    def enable_output_method
      @output_method = true
      @old_print = Pry.config.print
      Pry.config.print = proc do |*args|
        Hirb::View.view_or_page_output(args[1]) || @old_print.call(*args)
      end
    end

    def disable_output_method
      Pry.config.print = @old_print
      @output_method = nil
    end
  end

  Hirb.enable
end

erb2haml

rake haml:replace_erbs

確認用ページ作成

rails g controller welcome index

figaro

bundel exec figaro install
vi config/application.yml
figaro heroku:set -e production