rails s 実行時に、Address already in use とエラーがでるときの対処法

状態

rails sで、サーバー立ち上げようとするが、エラー発生。

Address already in use - bind(2) for "127.0.0.1" port 3000 (Errno::EADDRINUSE)

 

「ローカルサーバー3000番ポートがすでに使われている」ためのエラーのよう...

 

対処

1. lsof -i:3000でポート3000番を使用しているプロセスを確認

$ lsof -i:3000
COMMAND  PID USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
ruby    1444 user   19u  IPv4 0x1e75dcaef29d670b      0t0  TCP localhost:hbci (LISTEN)
ruby    1444 user   20u  IPv6 0x1e75dcaeebb77103      0t0  TCP localhost:hbci (LISTEN)
ruby    1444 user   25u  IPv6 0x1e75dcaeebb76583      0t0  TCP localhost:hbci->localhost:50195 (CLOSE_WAIT)
ruby    1444 user   26u  IPv6 0x1e75dcaeebb75fc3      0t0  TCP localhost:hbci->localhost:50196 (CLOSE_WAIT)
 
2. kill -9 1444でプロセスを kill する。

 続いて、lsof -i:3000でプロセスを確認

$ kill -9 1444
$ lsof -i:3000
=> なし

3. 再度、rails s

$ rails s
=> Booting Puma
=> Rails 5.2.3 application starting in development
=> Run `rails server -h` for more startup options
Puma starting in single mode..

 

結果

 ・無事、動作しました。 ^ - ^

 

 

19/2/19 [rails] gem 'devise'をインストールする手順

  1. Gemfileに下記を追記
    gem 'devise'

  2. $ bundle install
    deviseが入ったことを確認

  3. $ rails g devise:install

  4. データベースにusersテーブルを作りたいとき
    (Userモデルをつくる)
    $ rails g devise User

  5. bundle exec rake db:migrate
    4.で作成されたモデル、マイグレーションの内容でusersテーブルを作成

 

●「devise」用のビューファイルを作る

deviseではデフォルトのビューがある

このデフォルトのビューを編集したい場合の手順
$ rails g devise:views

でビューの作成

以下、作成されるもの

app/views/devise/confirmations/new.html.erb
app/views/devise/passwords/edit.html.erb
app/views/devise/passwords/new.html.erb
app/views/devise/registrations/edit.html.erb
app/views/devise/registrations/new.html.erb
app/views/devise/sessions/new.html.erb
app/views/devise/unlocks/new.html.erb
app/views/devise/mailer/confirmation_instructions.html.erb
app/views/devise/mailer/email_changed.html.erb
app/views/devise/mailer/password_change.html.erb
app/views/devise/mailer/reset_password_instructions.html.erb
app/views/devise/mailer/unlock_instructions.html.erb

この時点では、hamlでなく、erbでビュー表示確認できる。

次、hamlに変換してみる。

$ rake haml:replace_erbs

完了。

動作確認問題なし。

 

●サインアップ機能の追加

ユーザー名のような、deviseのデフォルトではないカラムのデータを保存したい場合

 


コントローラでアクションを設定 application_controller.rb
 ストロングパラメータも追加

class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception
  before_action :authenticate_user!
  before_action :configure_permitted_parameters, if: :devise_controller?

  protected
  def configure_permitted_parameters
    devise_parameter_sanitizer.permit(:sign_up, keys: [:name])
  end
end

 

 

 

 

 

 

 

 

 

 

 

19/2/19 rails haml導入、erbをhamlへ一括変換

Gemfile

gem 'haml-rails'
gem 'erb2haml'

 

bundle install

次に、拡張子がerbのファイルをhamlに変換します。

erb2hamlをインストールしてあるので、ターミナルで下記のコマンドを実行すれば一括で変換できます。

 

$ rake haml:replace_erbs

 

19/2/10 rails g controller の不要なファイルを生成されないようにする方法

rails g controller hogehogesを実行すると_test,_helper,_coffeeなど使用しないファイルまで生成してしまう。

 

設定を config/apprication.rbに追加する

module ChatSpace
class Application < Rails::Application
config.generators do |g|
g.stylesheets false
g.javascripts false
g.helper false
g.test_framework false
end
end
end

 

-------------------------------------------

$ rails generate controller Messages index

Running via Spring preloader in process 2904

      create  app/controllers/messages_controller.rb

       route  get 'messages/index'

      invoke  haml

      create    app/views/messages

      create    app/views/messages/index.html.haml

      invoke  assets

      invoke    coffee

      invoke    scss

-------------------------------------------