20210507のRailsに関する記事は17件です。

僕がWeb開発の道に進もうと思った2つのきっかけ

初めに 皆さんはどんな感じでプログラミングを始めましたか? 大半の方が ・稼ぎたい ・こんなものを作りたい のどちらかだと思いますが僕は最初はロボットを作りたいと思っていたのですが、当時プログラミング言語の違いなどあまり知らず適当にプログラミング言語の本を買ったらRailsの本でそのままweb開発の道に進むことになりました。 そんな薄っぺらい事情で始めた僕がWebプログラマーになろうと決心した二つのきっかけを紹介します。 1.Reddit Place Reddit Placeというサイト(プロジェクト)知った時は本当に鳥肌たちました。。 Reddit Placeは巨大な白紙データに、参加者が1ピクセルずつ好きな絵を描いていくというものなんですけど その時に掲示板上で「みんなでこの絵書こうぜ!」とか「みんなで一面黒に染めてやろうぜ!」とか 違う目的を持った二つの集団がぶつかり合ったりするドラマがあって 72時間の間に、こんな絵が描かれたんですよ。。 まじですごくないですか?顔も名前も知らない人たちが何百人単位で集まって協力して何か成し遂げさせるサイトって。。。 僕も人と人とが繋がり合って奇跡・ドラマが起こるようなサイト(アプリ)作りたいなぁと思ってweb開発が更に好きになりました。 そんな72時間のドラマを4:30にまとめたタイムラプス動画があるのでそちらもぜひ 詳しい記事 2.Facebook創設者 天才ザックバーグはこうして育てられた もう一つ感銘を受けたのがこの動画です。プログラマーは全員見たほうがいいです。 Facebook創設者マークザッカーバーグの育てられ方で父の「大事なのは人と人との繋がりなんだ」って言葉を聞いてまた鳥肌立ちました。 Reddit Placeもそうですが人が最も影響を受けるものって人なんですね 自分もオリジナルのSNSを作りたいと思うようになりました。 僕もこんな人生歩みたいなぁ まとめ もうまとめです。この記事を書いて分かったことはあんまり文章で感動って伝わらないんですね。。。 この二つを見たときはまじで震えたんですけどね。文章力鍛えます( ;∀;) 僕のAmmotアカウント 僕のツイッターアカウント https://twitter.com/yamada1531
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

[AWS]Aws::Sigv4::Errors::MissingCredentialsError: Cannot load `Rails.config.active_storage.service`:

はじめに 未解決エラーについて一旦忘れないようにこちらに記載します [追記]解決しました!!とてもしょうもないミスでした、、、、 環境 ruby 2.6.5 Rails 6.0.0 エラー内容 こちらの記事を参考に、AWSへもデプロイをしていこうと進めておりましたところ、Unicorn起動がうまく行かず、、、、 [tochikawa@ip-10-0-0-132 log]$ bundle exec unicorn_rails -c /var/www/rails/soccer_app/config/unicorn.conf.rb -D -E production master failed to start, check stderr log for details とりあえずログを見てみることに、、、すると、気になる表示が!! Aws::Sigv4::Errors::MissingCredentialsError: Cannot load `Rails.config.active_storage.service`: missing credentials, provide credentials with one of the following options: あ〜credentialまわりでなんか起きているなああということで情報収集したところ、この記事に記載のことが原因かな〜と確信!! しかし、一向に治らず、、、 どうもcredentialsとmaseter.keyがうまくあってないような気がするんですが、ここは間違いなく大丈夫、、 何が原因わからないよ〜〜〜!!! [追記] 原因判明 原因は以下二つだったことが判明 1.credentials.yml.encにaccess_key等が設定されていなかった 2.storage.ymlのインデントミス 正直恥ずかしいまでに初歩的なミスです。 けれどおかげさまで、この辺りの知識メッッチャ増えました! おわりに 転職活動中のみですので、早く完成させたい!と思い焦ってしまいましたが、一度落ち着いてよくみてみるとたいしたことなかったりしますね笑
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

[AWS]未解決エラー:Aws::Sigv4::Errors::MissingCredentialsError: Cannot load `Rails.config.active_storage.service`:

はじめに 未解決エラーについて一旦忘れないようにこちらに記載します 環境 ruby 2.6.5 Rails 6.0.0 エラー内容 こちらの記事を参考に、AWSへもデプロイをしていこうと進めておりましたところ、Unicorn起動がうまく行かず、、、、 [tochikawa@ip-10-0-0-132 log]$ bundle exec unicorn_rails -c /var/www/rails/soccer_app/config/unicorn.conf.rb -D -E production master failed to start, check stderr log for details とりあえずログを見てみることに、、、すると、気になる表示が!! Aws::Sigv4::Errors::MissingCredentialsError: Cannot load `Rails.config.active_storage.service`: missing credentials, provide credentials with one of the following options: あ〜credentialまわりでなんか起きているなああということで情報収集したところ、この記事に記載のことが原因かな〜と確信!! しかし、一向に治らず、、、 どうもcredentialsとmaseter.keyがうまくあってないような気がするんですが、ここは間違いなく大丈夫、、 何が原因わからないよ〜〜〜!!! おわりに 現在、原因究明中 どなたかお分かりの方いれば是非コメントを!!
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

visudo: /etc/sudoers がビジー状態です。後で再試行してくださいエラーへの対応

はじめに ポートフォリオサイトをAWSへデプロイする際に上記エラーが発生したため、その時の解決手順を記載します。 今回は「sudo visudo」コマンドを入力した際にエラー表示され、ターミナルでのコマンド入力が何もできなくなってしまいました。 エラー内容としては他で既にvisudoが起動しているから重複して起動できませんよーといったものだが、他で起動した記憶が全くない状況でした。 後々考えると、一度ターミナルを強制終了していたので、おそらくそのせいだと思います。 解決に向けて 結論からいくと、プロセスIDが正常に切断されていなかったことが原因でした。 そのためプロセスIDを消去してあげる必要があります。 ①ターミナル上でルートディレクトリに移動。 ※どの状態でエラーが発生したかによって、実行するディレクトリも異なってきます。例えばec2-userの状態でエラーが発生したのであれば、ルートディレクトリではなく、ec2-userにて削除を実施する必要があります。 ターミナル *****@***** ~ % *はPCのユーザー情報 ②psコマンドにてプロセスIDの確認 ターミナル *****@***** ~ %ps ③プロセスIDの削除 ②のコマンド入力で、 ターミナル PID TTY TIME CMD 10490 ttys000 0:00.14 -zsh のような表示がされると思います。 そこで該当のPID(プロセスID)を削除します。 削除するコマンドは、killもしくはkill-9となります。(kill-9は強制的に終了するコマンド) ターミナル *****@***** ~ %kill PIDの値 もしくは kill-9 PIDの値 プロセスID削除後に再度試すとエラーが発生せず動作しました。
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

【rails】"該当のIDがない場合"のroutes

状況 ECサイトの商品詳細ページを検索した時、存在しない商品(該当のidのない、showメソッド)を検索した時、専用のページを表示したい。 実際の記述 routes.rb resources :products #showアクションを定義 get "/products/not_find", to:"products#not_find" # not_findアクションを定義 products_controller.rb def show unless Product.exists?(params[:id]) render products_not_find_path end end ↑unless Product.exists?(params[:id])で該当のIDが存在しないかチェック。(存在しなければ 中身を実行) render products_not_find_path でnot_findページへ飛ばす。
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

Rails Gem JSONをシリアライズする active_model_serializersを学ぶ

はじめに RailsでAPIモードでアプリケーションを作ったことすらないですが active_model_serializersというGemについて学んだことをまとめておきます。 シリアライズとは シリアライズとは、複数の並列データを直列化して送信することである。 具体的には、メモリ上に存在する情報を、ファイルとして保存したり、ネットワークで送受信したりできるように変換することである。他方、既にファイルとして存在しているデータや、一旦シリアライズされたデータがネットワークから送られてきた際に、プログラムで扱えるようにする作業をデシリアライズと呼ぶ。 とのことですが、データをJSONで返すのもシリアライズされた結果のようです。 今回は置いておきます。。    active_model_serializersとはどんなGem? active_model_serializersとはJSONを簡単にいい感じに整形してくれるGemらしいです。 他にも同じようなGemやjbuilder(APIの記事によく出てきました)のような機能はありますが レスポンスが早い SerializerクラスにJSONを整形する処理をまとめられる 直感的にわかりやすい書き方ができる というメリットがあるようです。 active_model_serializersの導入 Gemfileに記述してbundle installして導入します。 Gemfile gem ' active_model_serializers' % bundle install モデル生成 % rails g serializer モデル名 このコマンドを実行することで、appディレクトリ下にserializersディレクトリが自動的に作成され、 指定したモデル名のファイルも作成されます。 例えば console % rails g serializer user とするとuser_serializer.rbが作成されます。 自動生成されたserializerファイルにいろいろな記述をすることで自由にJSONを整形することができます。 JSONに含める値を追加、削除する app/serializers/user_serializer class UserSerializer < ActiveModel::Serializer attributes :id end 初期状態ではこのような形になっていますが ここに表示したいモデルのカラム名を入れることでJSONに含めることができます。 app/serializers/user_serializer class UserSerializer < ActiveModel::Serializer attributes :id, :name, :email end nameとemailを追加しています。 controllerにシリアライザーを指定します ruby:users_controller.rb class UsersController < ApplicationController def index users = User.all #このように指定します render json: users, each_serializer: UserSerializer end end json { "id": 1, "name": "Tom", "email": Tom@example.com } まだまだ理解が足りないので 随時追加していきます。 参考サイト Rails 6で認証認可入り掲示板APIを構築する #9 serializer導入 gem Active Model Serializers のドキュメントを翻訳しました Rails – ActiveModelSerializers gemでサクサクAPI開発 Railsのactive_model_serializerについて学ぶ_100DaysOfCodeチャレンジ10日目(Day_10:#100DaysOfCode)
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

[Rails] gem 'mailcatcher' をインストールした際に起きたエラーの対処

■ 本記事の概要 開発環境下でメールが正しく送信できているかを確認するために、gem 'mailcatcher'を利用した際に遭遇したエラーの紹介と解決方法になります。 ■ 開発環境 OS:macOS Big Sur 11.3.1 Ruby:3.0.0 Ruby on Rails:6.1.3 ローカル環境DB:Mysql テキストエディタ:Visual Studio Code ■ 起きたエラー mailcatcherはbundleでインストールすると上手く動かないことがあるそうなので、ターミナルからgem install mailcatcherを入力してインストールしました。 ターミナル $ gem install mailcatcher すると、以下のようなエラー文が出力されてきました。 ターミナル Building native extensions. This could take a while... ERROR: Error installing mailcatcher: ERROR: Failed to build gem native extension. 〜〜〜 省略 〜〜〜 parser.rl:144:14: error: implicit declaration of function 'thin_http_parser_is_finished' is invalid in C99 [-Werror,-Wimplicit-function-declaration] } else if (thin_http_parser_is_finished(parser) ) { ^ 1 warning and 2 errors generated. make: *** [parser.o] Error 1 make failed, exit code 2 〜〜〜 省略 〜〜〜 ■ 解決 ERROR: Failed to build gem native native extension.の方で検索をかけて調べてみると、Xcode関連の問題かとも思ったのですが、感覚的に違うところが原因ではないかと思い、Error installing mailcatcherの方で再度調べてみました。 すると、mailcatcherの公式Githubに寄せられたissuesが引っかかったので、中を覗いてみると今回と同様のエラーが起きているようでした。 参考先:Installation Fails on Catalina #430 ( https://github.com/sj26/mailcatcher/issues/430 ) そこに寄せられたコメントによると、gem install mailcatcherを入力する前に、gem install thin -v '1.5.1' -- --with-cflags="-Wno-error=implicit-function-declaration"を行えばインストールできるようになると書かれていました。 実際に以下の順で入力したところ、無事にインストールできました。 ターミナル $ gem install thin -v '1.5.1' -- --with-cflags="-Wno-error=implicit-function-declaration" $ gem install mailcatcher . (インストールの確認が取れた後、件のGitHubのREADME.mdを読んでみたら、thinでエラーが発生したら先にgem install thin -v '1.5.1' -- --with-cflags="-Wno-error=implicit-function-declaration"を試してみるようにとの記載がありました) ■ 設定 gem 'mailcatcher'は無事にインストールできたので、公式GitHub( https://github.com/sj26/mailcatcher ) に習って、environments/development.rb に以下の記述を加えます。 environments/development.rb config.action_mailer.delivery_method = :smtp config.action_mailer.smtp_settings = { :address => '127.0.0.1', :port => 1025 } config.action_mailer.raise_delivery_errors = false ■ 起動 Rails側の設定も完了したので、mailcatcherを起動した後、rails sでアプリケーションを立ち上げます。 ターミナル $ mailcatcher <%# 入力すると以下がターミナルに表示されます %> Starting MailCatcher ==> smtp://127.0.0.1:1025 ==> http://127.0.0.1:1080/ *** MailCatcher runs as a daemon by default. Go to the web interface to quit. ターミナル $ rails s ■ 確認&終了 ブラウザを操作していってメール送信がされた後、http://127.0.0.1:1080/にアクセスして、ちゃんと送信されているかを確認します。 (以下http://127.0.0.1:1080/にアクセスした際の画面と終了ボタン) 送信されたメールの中身(HTML, Text)を確認できたので、mailcatcherを終了させます。終了のさせ方は、上記の画面上に表示されているQuitをクリックすると、mailcatcherは終了します。 (再度メールの確認をする際は、ターミナルからmailcatcherを起動させる必要があります。 最後に 以上が今回私が行った対処になります。 同じようなエラーが発生した方の参考になれば嬉しいです! 最後に ここまで読んでいただき、ありがとうございました!!
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

Railsチュートリアルで早速Railsがインストールできなかった話

背景 Railsのチュートリアルをチュートリアル通り進めると、早速Railsのインストールでエラーが出たので、対処法。 実行したコマンド gem install rails -v 6.0.3 エラーの内容 ERROR: Error installing rails: ERROR: Failed to build gem native extension. current directory: /Users/tomoki.katsu/.rbenv/versions/2.6.0/lib/ruby/gems/2.6.0/gems/mimemagic-0.3.10/ext/mimemagic /Users/tomoki.katsu/.rbenv/versions/2.6.0/bin/ruby -rrubygems /Users/tomoki.katsu/.rbenv/versions/2.6.0/lib/ruby/gems/2.6.0/gems/rake-12.3.2/exe/rake RUBYARCHDIR\=/Users/tomoki.katsu/.rbenv/versions/2.6.0/lib/ruby/gems/2.6.0/extensions/x86_64-darwin-19/2.6.0/mimemagic-0.3.10 RUBYLIBDIR\=/Users/tomoki.katsu/.rbenv/versions/2.6.0/lib/ruby/gems/2.6.0/extensions/x86_64-darwin-19/2.6.0/mimemagic-0.3.10 rake aborted! Could not find MIME type database in the following locations: ["/usr/local/share/mime/packages/freedesktop.org.xml", "/opt/homebrew/share/mime/packages/freedesktop.org.xml", "/opt/local/share/mime/packages/freedesktop.org.xml", "/usr/share/mime/packages/freedesktop.org.xml"] Ensure you have either installed the shared-mime-info package for your distribution, or obtain a version of freedesktop.org.xml and set FREEDESKTOP_MIME_TYPES_PATH to the location of that file. This gem might be installed as a dependency of some bigger package, such as rails, activestorage, axlsx or cucumber. While most of these packages use the functionality of this gem, some gems have included this gem by accident. Set USE_FREEDESKTOP_PLACEHOLDER=true if you are certain that you do not need this gem, and wish to skip the inclusion of freedesktop.org.xml. The FREEDESKTOP_PLACEHOLDER option is meant as a transitional feature, and will be deprecated in the next release. Tasks: TOP => default (See full trace by running task with --trace) rake failed, exit code 1 Gem files will remain installed in /Users/tomoki.katsu/.rbenv/versions/2.6.0/lib/ruby/gems/2.6.0/gems/mimemagic-0.3.10 for inspection. Results logged to /Users/tomoki.katsu/.rbenv/versions/2.6.0/lib/ruby/gems/2.6.0/extensions/x86_64-darwin-19/2.6.0/mimemagic-0.3.10/gem_make.out 調べてみるとどうやら、shared-mime-infoをインストールするか、インストールするRailsのバージョンを変更する必要があるみたい 解決方法 解決方法は以下2通り インストールするバージョンを変更 Rails 5.2.5 / 6.0.3.6 / 6.1.3.1をインストールする shared-mime-infoをインストールする 下記コマンドでshared-mime-infoをインストールします。 brew install shared-mime-info 参考記事
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

[Rails/devise] アカウント情報の編集・変更機能の実装

概要 deviseでユーザー管理機能を実装した際、 「ユーザーがパスワードやアイコンの画像を編集・変更が行える機能は必須では・・・」 と考えられたため実装しました。deviseで作成したupdateの設定やapplication_controller.rbに追記を行えば簡単に実装できましたのでここに記述します。 環境 macOS Big Sur バージョン 11.2.3 Ruby 2.6.5 Rails 6.0.3.6 Mysql Ver 14.14 Distrib 5.6.51 前提 deviseをインストールし、モデルを作成している。 ※この記事ではコントローラーの作成から始めます。 流れ 1.deviseのコントローラーを作成 2.edit.html.erbにformを記述 3.routes.rbにupdateのルーティングを記述 4.application_controller.rbにてupdateによるパラメーターの変更を許可する 5.registrations_controller.rbに上書き後の遷移先を記述 1.deviseのコントローラーを作成 deviseのコントローラーを作成します。 ターミナル. rails g devise:controllers users ↑ユーザー情報に関わるモデル名 これでregistrations_controller.rbを含むdeviseのコントローラーが作成されます。 2.edit.html.erbにformを記述 edit.html.erb <%= form_with model: @user, url:user_registration_path, class: 'registration-main', local: true do |f| %>                          ↑ 「rails routes」で表示されるusers/registrations#updateのPrefixを記述。 3.routes.rbにupdateのルーティングを記述 edit.html.erbで設定したボタンを押すとregistrations_controller.rbを経由してデータが上書きされるようにします。 application_controller.rb. Rails.application.routes.draw do devise_for :users, controllers: { sessions: "users/sessions", registrations: "users/registrations" # registrations_controller.rbに導くためのルーティングを記述 } # 中略 root to: 'photos#index' end registrations_controller.rbを経由するためのルーティングを設定しました。 4.application_controller.rbにてupdateによるパラメーターの変更を許可する sign_upの際に「初期設定のないカラムを保存するための許可」を記述されたと思われます。 updateの際も同様に「上書きする許可」を記述します。 今回は「nickname,imgae,password」の編集・変更機能を実装することが目的だったので以下のようにしました。 application_controller.rb class ApplicationController < ActionController::Base # 中略 private def configure_permitted_parameters devise_parameter_sanitizer.permit(:sign_up, keys: [:name, :nickname, :self_introduction, :image, :twitter, :facebook, :instagram]) devise_parameter_sanitizer.permit(:account_update, keys: [:nickname, :image]) #account_updateにつづいて編集・変更したいカラム名を入れる。 end # 中略 end これで編集・変更機能の内容は終わりました。最後に「上書きを行った後、どこに遷移するのか」を記述します。 5.registrations_controller.rbに上書き後の遷移先を記述 今回は上書き後に確認が行えるようcurrent_user(ログインしているユーザー)のマイページに遷移するようにします。 registrations_controller.rb class Users::RegistrationsController < Devise::RegistrationsController #中略 protected def after_update_path_for(resource) user_path(current_user) end #中略 これでregistrations#updateアクション後に、after_update_path_forのパスにリダイレクトします。以上 参考
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

【2021年現在】初心者でもご安心。chartkickのインストールの仕方(rails 6)

アプリケーションにグラフを差し込むのに便利なchartkickの説明(導入メイン)となります。 1.Chartkickの導入 gemfileに gem 'chartkick' を記述。その後忘れずにbundle installも! 続いて % yarn add chartkick chart.js ※こちらのコマンド忘れずに そしてapp/javascript/packs/application.js内に import "chartkick/chart.js" を記述する! ※もしグラフが上手く表示されない場合は app/javascript/packs/application.js内の記述を import "chartkick/chart.js"ではなく require("chartkick") require("chart.js") としてみてください。 rails5以前 の方は //= require chartkick //= require Chart.bundle こちらを。 2.グラフ表示 いよいよグラフ表示です! グラフを表示させたい「○○.html.erb」に <%= pie_chart [['赤', 50], ['青', 50]]%> と記述。 この段階で画面上に円グラフが表示されているか確認することで、chartkickがちゃんと導入できているか確認することができます。 ※もし上手く表示されない場合、『サーバー閉じてからのrails s』  もしくは『js内の記述を変更』してみてください 3.グラフについて pie_chartの部分を変更すればグラフの種類も変更可能です column_chart(縦棒グラフ) bar_chart(横棒グラフ) line_chart(折れ線グラフ) 今回は速攻でグラフが表示されるかを確認するためコントローラーを経由せず、ビュー内に直接データ記述(赤50青50)しました。 一般的な使い方としては コントローラ内にデータ記述する方法など色々ありますので下のリンクをご参考に。 参考サイト chartkick 【Rails】Rails完結!グラフを簡単に作成できるライブラリChartkickについて簡単にまとめてみた
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

binding.pryだけじゃなかったpry

はじめに デバッグについて調べていた際にpryのことをついて勉強。 binding.pryくらいしか使ってこなかったデバッグツールについて簡単にまとめておきます。 pryって? Rubyには標準ではirb(Interactive Ruby)が装備されていますが、 それをより便利にしたものになります。 初学者の方もirbを起動させてrubyのコードを打ったことがあるのではないでしょうか? Railsではデバッグ(バグなどを発見すること)をする際に使われます。 pryを使うにはGemfileにgemを追加する必要があります。 有名なものでpry-railsとpry-byebugがあります。 今回はpry-railsで説明します。 Gemfile gem 'pry-rails' Gemfileに追加して % bundle install bundle installしましょう。 これで使えるようになります。 pryを使うことのメリット 便利なコマンドが標準装備されている shellコマンドが使用可能 便利なコマンドが標準装備 よく使われるのはbinding.pryだと思います。 binding.pryとは 投稿フォームに入力した情報がデータベースに保存されないというエラーが発生したとします。 そういった場合、原因がどのコードにあるのかをbinding.pryという文字列をコントローラーなどのコード内に記述するとそこがブレイクポイントとなりrailsの処理を止めることができます。 その場所でpryが記述して変数の中身やパラメーターなどの確認をすることができます。 便利なコマンド一例 pryでは他に便利な機能が用意されていて helpと打つとコマンドの一覧が表示されます。 [7] pry(main)> help Help help Show a list of commands or information about a specific command. Context cd Move into a new context (object or scope). find-method Recursively search for a method within a class/module or the current namespace. ls Show the list of vars and methods in the current scope. pry-backtrace Show the backtrace for the pry session. raise-up Raise an exception out of the current pry instance. reset Reset the repl to a clean state. watch Watch the value of an expression and print a notification whenever it changes. whereami Show code surrounding the current context. :...skipping... Help help Show a list of commands or information about a specific command. Context cd Move into a new context (object or scope). find-method Recursively search for a method within a class/module or the current namespace. ls Show the list of vars and methods in the current scope. pry-backtrace Show the backtrace for the pry session. raise-up Raise an exception out of the current pry instance. reset Reset the repl to a clean state. watch Watch the value of an expression and print a notification whenever it changes. whereami Show code surrounding the current context. wtf? Show the backtrace of the most recent exception. Editing ! Clear the input buffer. amend-line Amend a line of input in multi-line mode. edit Invoke the default editor on a file. hist Show and replay readline history. play Playback a string variable, method, line, or file as input. show-input Show the contents of the input buffer for the current multi-line expression. Introspection ri View ri documentation. show-doc Show the documentation for a method or class. show-source Show the source for a method or class. :...skipping... Help help Show a list of commands or information about a specific command. Context cd Move into a new context (object or scope). find-method Recursively search for a method within a class/module or the current namespace. ls Show the list of vars and methods in the current scope. pry-backtrace Show the backtrace for the pry session. raise-up Raise an exception out of the current pry instance. reset Reset the repl to a clean state. watch Watch the value of an expression and print a notification whenever it changes. whereami Show code surrounding the current context. wtf? Show the backtrace of the most recent exception. Editing ! Clear the input buffer. amend-line Amend a line of input in multi-line mode. edit Invoke the default editor on a file. hist Show and replay readline history. play Playback a string variable, method, line, or file as input. show-input Show the contents of the input buffer for the current multi-line expression. Introspection ri View ri documentation. show-doc Show the documentation for a method or class. show-source Show the source for a method or class. stat View method information and set _file_ and _dir_ locals. Commands import-set Import a pry command set. :...skipping... Help help Show a list of commands or information about a specific command. Context cd Move into a new context (object or scope). find-method Recursively search for a method within a class/module or the current namespace. ls Show the list of vars and methods in the current scope. pry-backtrace Show the backtrace for the pry session. raise-up Raise an exception out of the current pry instance. reset Reset the repl to a clean state. watch Watch the value of an expression and print a notification whenever it changes. whereami Show code surrounding the current context. wtf? Show the backtrace of the most recent exception. Editing ! Clear the input buffer. amend-line Amend a line of input in multi-line mode. edit Invoke the default editor on a file. hist Show and replay readline history. play Playback a string variable, method, line, or file as input. show-input Show the contents of the input buffer for the current multi-line expression. Introspection ri View ri documentation. show-doc Show the documentation for a method or class. show-source Show the source for a method or class. stat View method information and set _file_ and _dir_ locals. Commands import-set Import a pry command set. Aliases !!! Alias for `exit-program` !!@ Alias for `exit-all` $ Alias for `show-source` /whereami[!?]+/ Alias for `whereami` :...skipping... Help help Show a list of commands or information about a specific command. Context cd Move into a new context (object or scope). find-method Recursively search for a method within a class/module or the current namespace. ls Show the list of vars and methods in the current scope. pry-backtrace Show the backtrace for the pry session. raise-up Raise an exception out of the current pry instance. reset Reset the repl to a clean state. watch Watch the value of an expression and print a notification whenever it changes. whereami Show code surrounding the current context. wtf? Show the backtrace of the most recent exception. Editing ! Clear the input buffer. amend-line Amend a line of input in multi-line mode. edit Invoke the default editor on a file. hist Show and replay readline history. play Playback a string variable, method, line, or file as input. show-input Show the contents of the input buffer for the current multi-line expression. Introspection ri View ri documentation. show-doc Show the documentation for a method or class. show-source Show the source for a method or class. stat View method information and set _file_ and _dir_ locals. Commands import-set Import a pry command set. Aliases !!! Alias for `exit-program` !!@ Alias for `exit-all` $ Alias for `show-source` /whereami[!?]+/ Alias for `whereami` ? Alias for `show-source -d` @ Alias for `whereami` file-mode Alias for `shell-mode` find-routes Alias for `find-route` :...skipping... Help help Show a list of commands or information about a specific command. Context cd Move into a new context (object or scope). Help help Show a list of commands or information about a specific command. Context cd Move into a new context (object or scope). find-method Recursively search for a method within a class/module or the current namespace. ls Show the list of vars and methods in the current scope. pry-backtrace Show the backtrace for the pry session. raise-up Raise an exception out of the current pry instance. reset Reset the repl to a clean state. watch Watch the value of an expression and print a notification whenever it changes. whereami Show code surrounding the current context. wtf? Show the backtrace of the most recent exception. Editing ! Clear the input buffer. amend-line Amend a line of input in multi-line mode. edit Invoke the default editor on a file. hist Show and replay readline history. play Playback a string variable, method, line, or file as input. show-input Show the contents of the input buffer for the current multi-line expression. Introspection ri View ri documentation. show-doc Show the documentation for a method or class. show-source Show the source for a method or class. stat View method information and set _file_ and _dir_ locals. Commands import-set Import a pry command set. Aliases !!! Alias for `exit-program` !!@ Alias for `exit-all` $ Alias for `show-source` /whereami[!?]+/ Alias for `whereami` ? Alias for `show-source -d` @ Alias for `whereami` file-mode Alias for `shell-mode` find-routes Alias for `find-route` history Alias for `hist` quit Alias for `exit` quit-program Alias for `exit-program` reload-method Alias for `reload-code` show-method Alias for `show-source` Input and output .<shell command> All text following a '.' is forwarded to the shell. cat Show code from a file, pry's input buffer, or the last exception. change-inspector Change the current inspector proc. change-prompt Change the current prompt. clear-screen Clear the contents of the screen/window pry is running in. fix-indent Correct the indentation for contents of the input buffer list-inspectors List the inspector procs available for use. save-file Export to a file using content from the repl. shell-mode Toggle shell mode. bring in pwd prompt and file completion. Misc pry-version Show pry version. reload-code Reload the source file that contains the specified code object. toggle-color Toggle syntax highlighting. Navigating pry !pry Start a pry session on current self. disable-pry Stops all future calls to pry and exits the current session. exit Pop the previous binding. exit-all End the current pry session. exit-program End the current program. jump-to Jump to a binding further up the stack. nesting Show nesting information. switch-to Start a new subsession on a binding in the current stack. Rails find-route See which urls match a given controller. recognize-path See which route matches a url. show-middleware Show all middleware (that rails knows about). show-model Show the given model. show-models Show all models. show-routes Show all routes in match order. (END) たくさんあります。 show-routes 例えばshow-routesというコマンドはルーティングを確認することができます。 ルーティングの確認だとrails routesがあるじゃんと思うかと思いますが show-routesはより高速に表示されます。 show-models `show-models`というコマンドはアプリケーションにある全てのモデル、カラム、データタイプを返してくれるコマンドです。 pryでshellコマンドを使う pryでshellコマンドをコンソール上で入力するには.をつけて実行しないとエラーになります。 終わりに なんとなく使っていたものもいろいろ便利な機能があるのだと思いました。 深掘りって大事ですね。 日々更新していきます。 参考サイト Pikawaka 【Rails】Pryについて徹底解説! TechTechMedia 【Rails】Rails開発者に絶対おすすめしたいデバッグ手法5選(初心者向け)
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

【個人開発】旅行の記録・共有サービス『TriSto』をリリースしました

はじめに 訪れた場所、使った金額、移動手段等を記録することで旅行の詳細を体系的にまとめることができる旅行の記録・共有サービス『TriSto』をリリースしました。 私は旅行に行った際、次の旅行の参考にしたり、思い出にふけったりするために、旅行の記録をスマホにメモしていたのですが、より詳細かつ体系的に記録をまとめたいという思いから、本サービスを開発いたしました。 私のようにとにかく事細かに旅行の記録を残したいという方や、他人の旅行記録を参考にしたいという方に、本サービスを活用して頂ければ幸いです。 サービス概要 旅行が好きな人に 旅行の記録を詳細に残せる場を提供する 旅行記録・共有サービス アプリURL:https://www.tristo.work/ GitHubリポジトリ:https://github.com/Manabu-Ito9772/tristo ターゲット 旅行の記録を詳細に残しておきたい方 他人の旅行記録を参照したい方 ユーザーが抱える課題 自分が行った旅行を詳細かつ分かりやすくまとめたい。 実際に旅行に行ってきた人の情報を、自分の旅行計画の参考にしたい。 望む未来 旅行の詳細情報を記録したり、他者の旅行記録を参考にしたりすることで、旅行好きの人がより良い旅行を実現し、さらに旅行を好きになってくれることを願っています。 旅行記録作成方法 1. ペンアイコンをクリック 2. 旅行の概要を入力して「詳細入力ページへ進む」をクリック 3. 詳細入力ページで日付ごとにブロックを追加 4. ブロックを追加できたら「投稿する」をクリック 5. 旅行記録が作成されタイムラインに投稿される ※タイムラインの旅行記録のタイトルをクリックすることで詳細を確認できる 使用技術 バックエンド Ruby 2.6.5 Rails 6.1.3.1 RSpec 3.10.1 主要なGem sorcery 0.16.0 jwt 2.2.2 active_model_serializers 0.10.12 kaminari 1.2.1 rails_admin 2.0.2 rspec-rails 5.0.1 ER図 フロントエンド Vue 2.6.12 axios 0.21.1 vue-router 3.5.1 vuex 3.6.2 vuex-persistedstate 4.0.0-beta.3 vue-mq 1.0.1 mobile-device-detect 0.4.3 vee-validate 3.4.5 vue-select 3.11.2 vue-dropdown-menu 0.1.4 vuejs-datepicker 1.6.2 vue2-timepicker 1.1.6 moment 2.29.1 vue-infinite-loading 2.4.5 vue-loader 15.9.6 vue-js-modal 2.0.0-rc.6 v-scroll-lock 1.3.1 vue-fontawesome 2.0.2 bootstrap 4.5.3 インフラストラクチャー Nginx 1.12.1 Unicorn 6.0.0 AWS VPC EC2 Amazon Linux 2 RDS MySQL 5.7.26 S3 ALB Route53 ACM インフラ構成図 工夫したところ 直感的なUI/UX 説明書がなくても直感的に操作できるシンプルで分かりやすいUI/UXにすることを心がけました。 SPA VueによるSPAを採用することで、さくさくとストレスなく操作できようにすることを心がけました。 レスポンシブデザイン スマートフォンでは指の位置を考慮してメニューバーをフッターに表示するなど、媒体ごとの使いやすさの違いを意識したレスポンシブなデザインにするように心がけました。 苦労したところ javascript 主にVueによる開発だったのですがあまりjavascriptの知識がなかったので、少し複雑な処理を実装する際に手間取りました。ただ、Rubyを学習済みであったためか、割とすんなり文法は理解できたので、開発の後半はjavascriptで詰まるということはほとんどありませんでした。 レイアウト 事前に作成した画面遷移図の通りにレイアウトを組み立ててもしっくりこないことが多く、納得のいくレイアウトができるまでだいぶ時間がかかりました。 学んだこと ググり方 普通にググって知りたいことを知れなかったときに、別のフレーズでググったり、関連するフレーズでググったり、英語でググったりすることで答えにたどり着くことがあったので、「いかにググるか」が非常に重要であるということが分かりました。 とくに英語でググるというのが最も有効で、日本語ではヒットしないけど英語だとヒットするみたいなことが何回かありました。当たり前といえば当たり前ですが日本語より英語の情報の方が圧倒的に多いので、プログラミングに限らず英語でのググり方に慣れればかなり世界が広がると感じました。 ライブラリの活用 ある機能を自力で実装している最中に同じ機能を簡単に実装できるライブラリを発見し、一瞬で実装できたことがありました。事前に調べてから実装を始めなかったことを反省しています。 先人の知恵にあやかり、極力車輪の再発明をしないことを心がけようと思いました。 詰まったら一旦リフレッシュ エラー解決などで詰まってしまったときに、一旦昼寝したり別のことをしたりしてから再度問題に取り組むことですぐに問題が解決することがありました。詰まってイライラしているときに無理に解決しようとせず、一旦リフレッシュして凝り固まった脳をほぐすということがわりと有効であるということが分かりました。 今後実装したいこと / 改善したいこと 今のところ手動でテスト・デプロイしているので、CapistranoやCircleCIを活用してCI/CDパイプラインを確立したいです。 可読性が低いコードや重複コードをリファクタリングしたいです。
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

【Rails】Herokuデプロイ時 We're sorry, but something went wrong.

はじめに デプロイが終わりサイトを開くと出てきた時のエラーです。 その為の解決方法を記載して行きます!^^ 解決方法 本番環境にデプロイした後にDBをマイグレートする必要があります! なので以下のコマンドを入力します。 ターミナル heroku run rails db:migrate これで無事エラーが解決されたと思います! herokuコマンド 以下にherokuコマンドを少し書いておきます(メモ書程度)? コマンド 内容 git push heroku master masterブランチをHerokuにデプロイ git push heroku ブランチ名:master master以外のブランチをHerokuにデプロイ git push heroku develop:master developブランチをHerokuにデプロイ git push heroku HEAD:master 現在のブランチをHerokuにデプロイ heroku create アプリ名 Herokuアプリを作成 heroku destroy Herokuアプリを削除 heroku open Herokuアプリをブラウザで開く heroku logs -t ログを確認 heroku run rails c コンソールを起動 heroku run rails db:migrate マイグレーションを実行 heroku run rails db:seed seeds.rbを実行 heroku addons:create アドオン名 アドオンを追加 heroku addons:create papertrail Papertrailを追加 heroku addons:create heroku-postgresql PostgreSQLを追加 heroku addons:open papertrail Papertrailをブラウザで開く heroku pipelines:open パイブライン名 パイプラインをブラウザで開く heroku apps Herokuのアプリ一覧を表示 heroku restart 再起動 heroku pg:reset データベースを削除し作り直す 最後に 日々コメダ珈琲にてプログラミング楽しく学習していますゴリあんです? 立派なエンジニアとして成長を何か貢献したいと考えております!
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

【Rails】Rails5のアプリでES6の構文を使用し,Herokuにデプロイする場合

はじめに Rails5アプリをHerokuにてのデプロイ作業を行っていた時にUglifier::Error: Unexpected token:~というエラーが発生した時に解決させた方法を記載して行きます。 環境 Ruby2.6.6 Rails 5.2.4 エラー内容 remote: ! remote: ! Precompiling assets failed. remote: ! remote: ! Push rejected, failed to compile Ruby app. remote: remote: ! Push failed remote: Verifying deploy... remote: remote: ! Push rejected to golfmatch. remote: To https://git.heroku.com/golfmatch.git ! [remote rejected] master -> master (pre-receive hook declined) error: failed to push some refs to 'https://git.heroku.com/アプリ名' Precompiling assets failed. プリコンパイルとはコンパイルする為にの事前処理(機械側が理解できる内容に処理) とりあえずファイルの読み込みができませんと言うわれています。^^; どこで処理が止まっているのかログを辿って行くと、 rake aborted! remote: Uglifier::Error: Unexpected token: punc ()). To use ES6 syntax, harmony mode must be enabled with Uglifier.new(:harmony => true). 「ES6を使うにはUglifier.new(:harmony => true)でharmony modeを有効にしてね」とのエラー内容です。 なので、 解決策 config/environments/production.rb config.assets.js_compressor = :uglifier ↓ 以下に変更 config.assets.js_compressor = Uglifier.new(harmony: true) 本番環境の設定ファイルを変更してあげれば無事でデプロイができました!^^ 終わりに 解決まで無事辿り着けたのもしっかりログを確認できたことでした! 文字の羅列がダーっと流れると読むのに嫌気が感じると思いますが、 ここです! rake aborted!と記してくれているので、 見つけたら解決までそう時間はかからないと思います! あと解決できると楽しいですしね^^ herokuデプロイにて困っている方の解決案になれば幸いです! ありがとうございました!?
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

RailsでCSV出力をviewに任せる実装

ほぼほぼこちらの記事の通りなのですが、自分でいろいろ試行錯誤する部分があったので自分用にメモ https://qiita.com/ichi_s/items/eddf29d62ac09f6f9133 rails new などは割愛しますがCSV用の出力のコントローラーを作成しエクスポート用のアクションを以下のように用意します csv_export_controller.rb class CsvExportController < ApplicationAdminController def index # index用のなにかしらの処理 end def export # Viewに渡すデータを用意 @users = Users.all # csvの中身の組み立てはviewで行います send_data(render_to_string(template: 'csv_export/export.csv.ruby', type: 'text/csv', filename: 'export_csv.csv')) end end views/export_csv/export.csv.ruby #require 'csv' # Gemを使いましょう # 出力に使うメソッドを定義すると呼び出せます(ヘルパーやデコレーターに書きましょう) def user_name(user) user.last_name + user.last_name end header = %w( 名前 住所 電話番号 メールアドレス ) # 実際の処理 CSV.generate(headers: header, write_headers: true) do |csv| @users.find_each do |user| row = [] row << user_name(user) row << user.address row << user.tel row << user.mail csv << row # sjisで出す場合、安全な文字に変換しとくと安心 # https://qiita.com/cog1t0/items/263ab21a25e2cbd2ecc7 end end # sjis # csv_data = CSV.generate(~) o # csv_data = csv_cata.encode(Encodinf::SJIS, { :invalid => :replace, :undef => :replace, :replace => ""} Railsに慣れきってると、シンプルなrubyファイルに書くの戸惑いますね(笑)
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

Failed to install gems via Bundler.」が出た時の対処法 HEROKU

herokuへプッシュしたいが下記のようなエラーが出た。 $ git push heroku main Enumerating objects: 113, done. Counting objects: 100% (113/113), done. Delta compression using up to 8 threads Compressing objects: 100% (95/95), done. Writing objects: 100% (113/113), 66.73 KiB | 4.17 MiB/s, done. Total 113 (delta 8), reused 0 (delta 0), pack-reused 0 remote: Compressing source files... done. remote: Building source: remote: remote: -----> Building on the Heroku-20 stack remote: -----> Determining which buildpack to use for this app remote: ! Warning: Multiple default buildpacks reported the ability to handle this app. The first buildpack in the list below will be used. remote: Detected buildpacks: Ruby,Node.js remote: See https://devcenter.heroku.com/articles/buildpacks#buildpack-detect-order remote: -----> Ruby app detected remote: -----> Installing bundler 2.2.16 remote: -----> Removing BUNDLED WITH version in the Gemfile.lock remote: -----> Compiling Ruby/Rails remote: -----> Using Ruby version: ruby-2.6.6 remote: -----> Installing dependencies using bundler 2.2.16 remote: Running: BUNDLE_WITHOUT='development:test' BUNDLE_PATH=vendor/bundle BUNDLE_BIN=vendor/bundle/bin BUNDLE_DEPLOYMENT=1 bundle install -j4 remote: Your bundle only supports platforms ["x86_64-darwin-20"] but your local platform remote: is x86_64-linux. Add the current platform to the lockfile with `bundle lock remote: --add-platform x86_64-linux` and try again. remote: Bundler Output: Your bundle only supports platforms ["x86_64-darwin-20"] but your local platform remote: is x86_64-linux. Add the current platform to the lockfile with `bundle lock remote: --add-platform x86_64-linux` and try again. remote: remote: ! remote: ! Failed to install gems via Bundler. remote: ! remote: ! Push rejected, failed to compile Ruby app. remote: remote: ! Push failed remote: ! remote: ! ## Warning - The same version of this code has already been built: cd642c3cdcf87def5c29b29dff759c88f1c0130b remote: ! remote: ! We have detected that you have triggered a build from source code with version cd642c3cdcf87def5c29b29dff759c88f1c0130b remote: ! at least twice. One common cause of this behavior is attempting to deploy code from a different branch. remote: ! remote: ! If you are developing on a branch and deploying via git you must run: remote: ! remote: ! git push heroku <branchname>:main remote: ! remote: ! This article goes into details on the behavior: remote: ! https://devcenter.heroku.com/articles/duplicate-build-version remote: remote: Verifying deploy... remote: remote: ! Push rejected to mysterious-plateau-98113. remote: To https://git.heroku.com/mysterious-plateau-98113.git ! [remote rejected] main -> main (pre-receive hook declined) error: failed to push some refs to 'https://git.heroku.com/mysterious-plateau-98113.git' 原因は、herokuのbundlerとローカルでのbundlerのバージョンに相違があることらしい。 解決方法 $ bundle lock --add-platform x86_64-linux # このコマンドはよく見るとエラー文のところに書いてあるからこのコマンドを実行する。 $ git add . # Gemfile.lockの変更をステージングへ $ git commit -m "Gemfile.lock fix" # コミットする $ git push heroku main # herokuへプッシュする これで解決できる。
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

Railsで使うdbコマンドまとめ

db:create # 使用中の環境でDBを作成する db:create # 全ての環境でDBを作成する db:create:all db:drop # 使用中の環境でDBを削除する db:drop # 全ての環境でDBを削除する db:drop:all db:migrate # 使用中の環境で未実行のマイグレーションを実行する db:migrate # 特定の一つのマイグレーションを実行する db:migrate:up # 特定の一つのマイグレーションの実行を取り消す db:migrate:down # 現在のマイグレーションの状態を表示する db:migrate:status # db:drop → db:create → db:migrate db:migrate:reset db:rollback # 最新のマイグレーションの実行を取り消す db:rollback db:seed # db/seed.rbに従ってレコードを作成する db:seed db:schema # 使用中の環境のDBでスキーマを読み込む db:schema:load # 使用中の環境のDBでスキーマを削除する db:schema:dump db:setup # db:schema:load → db:seed db:setup db:reset # db:drop → db:setup db:reset 参考文献
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む