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'をインストールする手順
- Gemfileに下記を追記
gem 'devise' - $ bundle install
deviseが入ったことを確認 - $ rails g devise:install
- データベースにusersテーブルを作りたいとき
(Userモデルをつくる)
$ rails g devise User - 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 データベース作成
databese.ymlの内容で、データベース作成
$ bundle exec rake db:create
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'
create app/views/messages
create app/views/messages/index.html.haml
invoke assets
invoke coffee
invoke scss
-------------------------------------------