【rails】Uncaught ReferenceError: $ is not defined
背景
Ajaxを組み込み、フォロー機能を実装している際に発生する。
エラーの原因は$ is not defined
より、jQueryに関連すると推測。
解決方法
- jQueryをGemfileに記載。
- app/assets/javascripts/applicationに
//= require jquery
を記載。 ただし、require_treeより前に組み込む。
今後への対策
rails new appをするだけで、jQueryはGemfileに自動で記載されているものと勘違いを起こしていた。
jQueryは、自分でgem "jquery-rails"
とGemfileに記述すること!!!
参考サイト
Ruby on Railsで、元のページにリダイレクトさせる方法
以下のコードを該当箇所に記載
redirect_back(fallback_location: root_path)
redirect_to :back を記述すると、HTTP_REFERERがないことでエラーになってしまう。
参考サイト
https://api.rubyonrails.org/classes/ActionController/Redirecting.html
Dockerを利用する際にbundle installをしたい場合のコマンド
''' $ docker-compose up --build '''
ActiveRecord::NoDatabaseError (Unknown database 'app_name_development'):エラーが出現時に役立ったコマンド
$ docker-compose run --rm web rake db:create $ docker-compose run web bin/rails db:migrate RAILS_ENV=development
Rails Tutorial Ch. 13でNameError: uninitialized constant Micropost::PictureUploaderが出た時の対処法
試したこと
springを再起動する
$ sudo spring stop $ bundle update
結果
無事にエラーが直りました
参考サイト
【13章】Railsチュートリアル中にNameError: uninitialized constant Micropost::PictureUploaderが出た時の対処法 - 紙一重の積み重ね
production環境でのメール認証でエラーが出現する際に試したこと
背景
herokuで作成中のwebアプリでユーザー登録を実行する際に、 sign upボタンを押すと、We're sorry, but something went wrong.が表示されるページにリダイレクトされてしまう。
heroku logs --tail
を実行してログの収集を行う。
原因
ログを追った結果、以下の文言を発見。
Net::SMTPFatalError (550 Unauthenticated senders not allowed)
SendGridでのエラーに起因しているようだ。 この文章をググると、以下の公式サイトが出てきた。 https://sendgrid.com/docs/for-developers/sending-email/smtp-errors-and-troubleshooting/#550-unauthenticated-senders-not-allowed
解決するには、以下を実行すれば良さそう。
you’ll want to make sure that you’ve configured your setup to connect to smtp.sendgrid.net using authentication, and that the credentials you’re using are the same credentials you use to login to the SendGrid.
解決方法
config/environments/production.rb SMPTの:user_nameを'SENDGRID_USERNAME'に、:passwordを'SENDGRID_PASSWORD'にそれぞれ変更。 そうすると、設定した通りにユーザー登録の為の認証メールが届いた。
注意事項として、本番運用するアプリケーションでは、暗号化されていないIDやパスワードのような重要なセキュリティ情報はソースコードに直接書き込まない。環境変数に記述し、そこからアプリケーションに読み込む。今回の場合は、そうした変数はSendGridアドオンが自動的に設定してくれる。
技術的に学べたこと
191126_日報
取り組んだこと
- It's Dangerous to Go Alone: Battling the Invisible Monsters in Tech youtu.be
- Believe you can change Believe you can change (Aaron Swartz's Raw Thought)
- codewar
- Rails Tutorial Ch.4:
学んだこと
“fixed mindset”
- the belief that your abilities are fixed and that the world is just a series of tests that show you how good you are
- success comes from proving how great you are.
- feel smart when they don't make mistakes
- try to blame the world when things go bad
- be afraid to try hard
"growth mindset"
- the belief that everything come through effort and that the world is full of interesting challenges that can help you learn and grow
- success comes from growing
- feel smart when they struggle with something for a long time and then finally figure it out
- look to see what they can change about themselves
- be afraid of not trying
this difference between these mindsets appear in all sorts of places - in relationships, CEO and even in sports.
your mindset itself can be changed
- the first step to getting better is believing you can get better
Rubyでクラスメソッドの定義
- self
mapメソッド
- ブロックの戻り値が新しい配列の各要素になる