LoginSignup
4
2

More than 5 years have passed since last update.

Railsの開発環境を整える②

Posted at

※2016年10月に別のブログで書いた記事を、移行したものになります。


新人SEである私が考えたベスト・プラクティスを、Ruby on Railsで実践してみるシリーズの2回目になります。

Railsの開発環境を整える① - Qiita
で環境は整ったので、今回は開発を進めて行きたいと思います。

RailsでBootstrapを使えるようにする。

Bootstrapは、デザイン知識がなくてもきれいなサイトを作れる、CSSフレームワークです。

使い方は、Bootstrap 3.0入門 (全18回) - プログラミングならドットインストールBootstrap · The world's most popular mobile-first and responsive front-end framework.を御覧ください。

まず、以下をGemfileに記述します。

gem 'bootstrap-sass'
gem 'autoprefixer-rails'
gem sass-rails

"autoprefixer-rails"は、自動でベンダープレフィックスを追加してくれます。

"gem sass-rails"は、すでに記述があれば不要です。

$ bin/bundle install

app/assets/stylesheets/ にある application.cssをapplication.css.scssに変更してから、以下の2行を追加。

@import "bootstrap-sprockets";
@import "bootstrap";

"bootstrap-sprockets"は、必ず先に書いてください。

app/assets/javascripts/application.js に以下を追加。

//= require bootstrap-sprockets

これで、Bootstrapが使えるようになります。

参考文献

タイムゾーンとロケールの設定

config/application.rbに、以下を追記

module MokumokuLibrary
class Application < Rails::Application
\# Settings in config/environments/* take precedence over those specified here.
\# Application configuration should go into files in config/initializers
\# -- all .rb files in that directory are automatically loaded.
<span style="color: #ff0000;">config.time_zone = 'Tokyo'
config.i18n.default_locale = :ja</span>
end
end

rspecコマンドを使えるようにする。

今回は、テストフレームワークにRSpecを使用します。
まず、rspecコマンドを、Spring経由で呼び出す準備をします。
Spring経由で呼び出すことで、実行時間を短くすることができます。

$ spring binstub rspec
WARN: Unresolved specs during Gem::Specification.reset:
minitest (~> 5.1)
WARN: Clearing out unresolved specs.
Please report a bug if this causes problems.
\* bin/rspec: generated with spring

エラーが起きたら、以下のコマンドで解決

$ gem cleanup

RSpecを利用する前に、一度だけ、以下のコマンドを実行する必要がある。

rails g rspec:install

テストフレームワークを、mini_testからRSpecに変更

module MokumokuLibrary
  class Application < Rails::Application
    \# Settings in config/environments/* take precedence over those specified here.
    \# Application configuration should go into files in config/initializers
    \# -- all .rb files in that directory are automatically loaded.
    config.time_zone = 'Tokyo'
    config.i18n.default_locale = :ja
    <span style="color: #ff0000;">config.generators do |g|
      g.test_framework :rspec
    end</span>
  end
end

Factory Girlの設定

spec/rails_helper.rbの以下を追記する。(RSpecでFactory Girlを利用するためには必要)
```
config.include FactoryGirl::Syntax::Methods

config.before(:suite) do
FactoryGirl.reload
end
```

localhost:3000にアクセスできるようにする

このままサーバーを立ち上げて、http://localhost:3000/ にアクセスしても、「このサイトにアクセスできません」となってしまいます。

config/boot.rbの末尾に、以下を追加

require 'rails/commands/server'
module Rails
class Server
def default_options
super.merge(Host: '0.0.0.0', Port: 3000)
end
end
end

これについては、以下を参照。
仮想サーバ上の WEBrick にローカルマシンから接続できない?! - Rails つまみぐい
Vagrant + rails + CentOS7の組み合わせでゲストOSのlocalhost:3000に接続できない場合 - Qiita

また、本来localhostというのは自分自身を指すので、仮想マシンにlocalhostで接続することはできません。
Vagrantfileに以下を追加します。

config.vm.network "forwarded_port", guest: 3000, host: 3000

ファイアフォールの3000portを開ける

$ sudo firewall-cmd --permanent --add-port=3000/tcp
success
$ sudo firewall-cmd --reload
success
$ firewall-cmd --list-all
public (default, active)
interfaces: enp0s3 enp0s8
sources:
services: dhcpv6-client http ssh
ports: 3000/tcp 8080/tcp
masquerade: no
forward-ports:
icmp-blocks:
rich rules:

http://localhost:3000/ にアクセスすると、デフォルトの画面が表示されました。

Ruby-on-Rails-1024x507.png

参考文献

GitHubでバージョン管理

以下のサイトを参考に、作成したRailsプロジェクトをGitHubで管理できる。

GitHubにIssueを作り、コミットを紐付ける

機能追加などは、機能単位でGitHubのIssueをつくる。
コミットにIssue番号を付けることで、Issueに対してどのようなコミットが行われたかを見ることができるようになる。

エラー解決

$ bin/rspec コマンド実行時

ActiveRecord::NoDatabaseError

データベースを作成する。

$ bin/rake db:create

マイグレーションファイルを作成しているなら、以下も実行

$ bin/rake db:migrate

NoMethodError

NoMethodError:
       undefined method `build' for #

<rspec::examplegroups::authenticator::authenticator:0x007f7937c39b98>

と出たら、
spec/rails_helper.rbの以下を追記する。(RSpecでFactory Girlを利用するためには必要)
```
config.include FactoryGirl::Syntax::Methods

config.before(:suite) do
FactoryGirl.reload
end
```
参考文献
実践Ruby on Rails 4 現場のプロから学ぶ本格Webプログラミング

rspecの実行でweb_consoleエラー

$ rspec spec/models/account_spec.rb
/home/vagrant/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/railties-5.0.0.1/lib/rails/railtie/configuration.rb:95:in `method_missing': undefined method `web_console' for #<rails::application::configuration:0x007fc495a2fb50> (NoMethodError)
Did you mean?  console
        from /home/vagrant/synced_folder/mokumoku_library/config/application.rb:22:in `<class:application>'
        from /home/vagrant/synced_folder/mokumoku_library/config/application.rb:10:in `<module:mokumokulibrary>'
        from /home/vagrant/synced_folder/mokumoku_library/config/application.rb:9:in `<top (required)="">'
        from /home/vagrant/synced_folder/mokumoku_library/config/environment.rb:2:in `require_relative'
        from /home/vagrant/synced_folder/mokumoku_library/config/environment.rb:2:in `<top (required)="">'
        from /home/vagrant/.rbenv/versions/2.3.0/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
        from /home/vagrant/.rbenv/versions/2.3.0/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
        from /home/vagrant/synced_folder/mokumoku_library/spec/rails_helper.rb:3:in `<top (required)="">'
        from /home/vagrant/.rbenv/versions/2.3.0/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
        from /home/vagrant/.rbenv/versions/2.3.0/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
        from /home/vagrant/synced_folder/mokumoku_library/spec/models/account_spec.rb:1:in `<top (required)="">'
        from /home/vagrant/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/rspec-core-3.5.4/lib/rspec/core/configuration.rb:1435:in `load'
        from /home/vagrant/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/rspec-core-3.5.4/lib/rspec/core/configuration.rb:1435:in `block in load_spec_files'
        from /home/vagrant/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/rspec-core-3.5.4/lib/rspec/core/configuration.rb:1433:in `each'
        from /home/vagrant/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/rspec-core-3.5.4/lib/rspec/core/configuration.rb:1433:in `load_spec_files'
        from /home/vagrant/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/rspec-core-3.5.4/lib/rspec/core/runner.rb:100:in `setup'
        from /home/vagrant/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/rspec-core-3.5.4/lib/rspec/core/runner.rb:86:in `run'
        from /home/vagrant/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/rspec-core-3.5.4/lib/rspec/core/runner.rb:71:in `run'
        from /home/vagrant/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/rspec-core-3.5.4/lib/rspec/core/runner.rb:45:in `invoke'
        from /home/vagrant/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/rspec-core-3.5.4/exe/rspec:4:in `<top (required)="">'
        from /home/vagrant/.rbenv/versions/2.3.0/bin/rspec:22:in `load'
        from /home/vagrant/.rbenv/versions/2.3.0/bin/rspec:22:in `<main>'

config/applicationの以下の箇所をコメントアウト。
```

config.web_console.whitelisted_ips = '10.0.2.2'

動いた。

$ rspec spec/models/account_spec.rb
.

Finished in 0.14284 seconds (files took 13.47 seconds to load)
1 example, 0 failures
```
GitHubを見返してみると、Rails newしたときにはまだなくて、自分で書いた記憶もない。何かのコマンドを実行した時か、Gemでライブラリ読み込んだ時に、書き込まれるのか?
web consoleとは、ブラウザがコンソールになる的なものらしい。

Git addで改行コードエラー

warning: LF will be replaced by CRLF in app/models/account.rb.
The file will have its original line endings in your working directory.

git add した時に出る改行コードのwarning対応 - 先がまっくろーを参考に解決できる。

4
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
2