20220111のRailsに関する記事は7件です。

Ruby on Rails 日時データを年月日の形式で表示する

日時データを年月日で表示したい scheduleというテーブルの中から、startという日時データのカラムの値を表示したい時、 <%= schedule.start %> ↑こんな感じで書くと ↑こんな感じで表示される。 これを、年月日だけで表示していきたい。 対処法 <%= schedule.start&.strftime("%Y年 %m月 %d日") %> ↑このように書くと、 ↑こんな感じで表示される。 時分秒まで表示する 更新日時(updated_at)など、何時何分何秒の部分を表示したい時は、 <%= schedule.updated_at&.strftime("%Y年 %m月 %d日 %H時 %M分") %> こんな感じの書き方になる。
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

【Rails】ActionController::RoutingError (uninitialized constant HogeControllerの対処法

症状 railsで新たにアクションを追加したときに下記エラーメッセージが表示されました。 翻訳すると「初期化されていない定数hogeController」でした。 error ActionController::RoutingError (uninitialized constant HogeController routeファイルとhoges_contollerファイルは以下です。 routes.rb post 'hoge', to: 'hoge#update' hoges_contoller class HogesController < ApplicationController end 解決方法 routeファイルのhogeをhogesに変更することで解決しました。 route.rb post 'hoge', to: 'hoges#update' 下記の記事ではファイル名が間違っていたため同様のエラーが出ていたようです。 このエラーでは、ファイル名やルーティングの名前が正しくないと出てくるようです。 参考
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

【Rails】dynamic constant assignmentの対処法【Ruby】

症状 railsで以下エラーが発生しました。 翻訳すると、「動的定数の割り当て」でした。 error SyntaxError (C:/app/controllers/api/v1/users_controller.rb:21: dynamic constant assignment YOUR_DOMAIN = 'http://localhost:3000' ^~~~~~~~~~~ users_controller.rb def update user = User.find_by(id: params[:userId]) DOMAIN = 'http://localhost:3000' if(user.save!(user_params) redirect_to DOMAIN + '?success=true', else redirect_to DOMAIN + '?canceled=true', end end 解決方法 ApplicationControllerに移動して、解決しました。 定数をアクション内で書いたから怒られていたようです。 application_controller.rb DOMAIN = 'http://localhost:3000' 参考
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

Ruby3.1.0のbundle installでpuma5.5.2がダウンロードできなかった時

環境 macOS 12.1 (Monteray) ruby 3.1.0p0 (2021-12-25 revision fb4df44d16)[arm64-darwin21] Rails 7.0.1 Apple clang version 13.0.0 (clang-1300.0.29.30) 要約 bundle config build.puma CC="clang -fdeclspec" でbundle installの実行時にオプションを渡して bundle install を実行。 出ていたエラーメッセージなど Rails7.0.1でrails newした後Gemを入れようとしてbundle installを実行するとこんな感じのエラーが発生しました。(一部省略) current directory: /rails/rails7-try/vendor/bundle/ruby/3.1.0/gems/puma-5.5.2/ext/puma_http11 /.anyenv/envs/rbenv/versions/3.1.0/bin/ruby -I /.anyenv/envs/rbenv/versions/3.1.0/lib/ruby/3.1.0 -r ./siteconf20220111-40100-3sw1db.rb extconf.rb --with-cflags\=-Wno-error\=implicit-function-declaration checking for BIO_read() in -lcrypto... *** extconf.rb failed *** Could not create Makefile due to some reason, probably lack of necessary libraries and/or headers. Check the mkmf.log file for more details. You may need configuration options. Provided configuration options: --with-opt-dir --without-opt-dir --with-opt-include --without-opt-include=${opt-dir}/include --with-opt-lib --without-opt-lib=${opt-dir}/lib --with-make-prog --without-make-prog --srcdir=. --curdir /.anyenv/envs/rbenv/versions/3.1.0/bin/$(RUBY_BASE_NAME) --with-puma_http11-dir --without-puma_http11-dir --with-puma_http11-include --without-puma_http11-include=${puma_http11-dir}/include --with-puma_http11-lib --without-puma_http11-lib=${puma_http11-dir}/lib --with-openssl-dir --with-openssl-include --without-openssl-include=${openssl-dir}/include --with-openssl-lib --without-openssl-lib=${openssl-dir}/lib --with-openssl-config --without-openssl-config --with-pkg-config --without-pkg-config --with-crypto-dir --without-crypto-dir --with-crypto-include --without-crypto-include=${crypto-dir}/include --with-crypto-lib --without-crypto-lib=${crypto-dir}/lib --with-cryptolib --without-cryptolib /.anyenv/envs/rbenv/versions/3.1.0/lib/ruby/3.1.0/mkmf.rb:498:in `try_do': The compiler failed to generate an executable file. (RuntimeError) You have to install development tools first. To see why this extension failed to compile, please check the mkmf.log which can be found here: /rails/rails7-try/vendor/bundle/ruby/3.1.0/extensions/arm64-darwin-21/3.1.0/puma-5.5.2/mkmf.log extconf failed, exit code 1 Gem files will remain installed in /rails/rails7-try/vendor/bundle/ruby/3.1.0/gems/puma-5.5.2 for inspection. Results logged to /rails/rails7-try/vendor/bundle/ruby/3.1.0/extensions/arm64-darwin-21/3.1.0/puma-5.5.2/gem_make.out An error occurred while installing puma (5.5.2), and Bundler cannot continue. In Gemfile: puma ログを見ろと言っているのでログを見ているとこんな感じ(一部省略)になっており、どうやらパラメータを渡せとのこと。 /.anyenv/envs/rbenv/versions/3.1.0/include/ruby-3.1.0/ruby/assert.h:132:1: error: '__declspec' attributes are not enabled; use '-fdeclspec' or '-fms-extensions' to enable support for __declspec attributes RBIMPL_ATTR_NORETURN() ^ ^ /.anyenv/envs/rbenv/versions/3.1.0/include/ruby-3.1.0/ruby/internal/attr/noreturn.h:29:33: note: expanded from macro 'RBIMPL_ATTR_NORETURN' # define RBIMPL_ATTR_NORETURN() __declspec(noreturn) ^ fatal error: too many errors emitted, stopping now [-ferror-limit=] 20 errors generated. checked program was: /* begin */ 1: #include "ruby.h" 2: 3: int main(int argc, char **argv) 4: { 5: return !!argv[argc]; 6: } /* end */ どういうふうに渡せば良いのかわからないので調べていると下記の記事を発見したので参考にさせて頂きました。 ただbundle install時に渡したかったので bundle config build.puma CC="clang -fdeclspec" を実行してホームディレクトリ配下の.bundle/configに値をセット。 ファイルを見ると BUNDLE_BUILD__PUMA: "CC=clang -fdeclspec" と追加されているのがわかります。 この状態で bundle install を実行すると問題なくインストールできました。 意外とハマったので共有します。
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

【Rails】同じscopeはmoduleに共通化しよう

複数のクラスに共通するscopeがあるとき、moduleとして共通化する。 before author.rb class Author < ApplicationRecord scope :active, -> { where(state: 'active') } end book.rb class Book < ApplicationRecord scope :active, -> { where(state: 'active') } end after models/concerns/active_scope.rb module ActiveScope extend ActiveSupport::Concern included do scope :active, -> { where(state: 'active') } end end author.rb class Author < ApplicationRecord include ActiveScope end book.rb class Book < ApplicationRecord include ActiveScope end RSpec モデルと同じようにテストする spec/models/active_scope_spec.rb require 'rails_helper' RSpec.describe ActiveScope, type: :model do describe '#active' do it '' do ... end end end 参考
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

【Rails】routesのエラー「``': No such file or directory 」の対処法

症状 railsで新たにアクションを追加しようと、routeファイルにルートを追加してサーバーを起動したところ下記のエラーが発生しました。 翻訳すると、「そのようなファイル、又はディレクトリはありません」でした error rails s => Booting Puma => Rails 6.0.4.1 application starting in development => Run `rails server --help` for more startup options Exiting D:/app/config/routes.rb:28:in ``': No such file or directory - checkout (Errno::ENOENT) from C:/appu/config/routes.rb:28:in `block (3 levels) in <main>' from C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/actionpack-6.0.4.1/lib/action_dispatch/routing/mapper.rb:956:in routesファイルの該当の箇所は以下です。 routes.rb post `checkout`, to: `stripes#checkout` 解決方法 ルーティングしている``を''に変更することで解決しました。 ファイルがないというより、指定の仕方が悪かったからファイルがないと怒られていたようです。 routes.rb post 'checkout', to: 'stripes#checkout' 参考
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

postgresqlのbundle installができない(sh: /usr/bin/pg_config: No such file or directory)

エラー内容 新規プロジェクト作成時、 rails new . --database=postgresql --skip-test をした後、bundle installをしようとしたらできなかった?   # エラー全文 Gem::Ext::BuildError: ERROR: Failed to build gem native extension. current directory: /Users/apple/workspace/runteq/muscle_beat/vendor/bundle/ruby/3.0.0/gems/pg-1.2.3/ext /Users/apple/.rbenv/versions/3.0.2/bin/ruby -I /Users/apple/.rbenv/versions/3.0.2/lib/ruby/3.0.0 -r ./siteconf20220111-87208-7szvjs.rb extconf.rb --with-pg-config\=/usr/bin/pg_config Using config values from /usr/bin/pg_config sh: /usr/bin/pg_config: No such file or directory sh: /usr/bin/pg_config: No such file or directory checking for libpq-fe.h... yes checking for libpq/libpq-fs.h... yes checking for pg_config_manual.h... yes checking for PQconnectdb() in -lpq... yes checking for PQsetSingleRowMode()... yes checking for PQconninfo()... yes checking for PQsslAttribute()... yes checking for PQresultVerboseErrorMessage()... yes checking for PQencryptPasswordConn()... yes checking for PQresultMemorySize()... yes checking for timegm()... yes checking for rb_gc_adjust_memory_usage()... yes checking for unistd.h... yes checking for inttypes.h... yes checking for C99 variable length arrays... yes creating extconf.h creating Makefile current directory: /Users/apple/workspace/runteq/muscle_beat/vendor/bundle/ruby/3.0.0/gems/pg-1.2.3/ext make DESTDIR\= clean current directory: /Users/apple/workspace/runteq/muscle_beat/vendor/bundle/ruby/3.0.0/gems/pg-1.2.3/ext make DESTDIR\= compiling gvl_wrappers.c compiling pg.c compiling pg_binary_decoder.c compiling pg_binary_encoder.c compiling pg_coder.c pg_coder.c:216:34: warning: implicit conversion loses integer precision: 'long' to 'int' [-Wshorten-64-to-32] res = this->dec_func(this, val, RSTRING_LEN(argv[0]), tuple, field, ENCODING_GET(argv[0])); ~~~~ ^~~~~~~~~~~~~~~~~~~~ /Users/apple/.rbenv/versions/3.0.2/include/ruby-3.0.0/ruby/internal/core/rstring.h:44:27: note: expanded from macro 'RSTRING_LEN' #define RSTRING_LEN RSTRING_LEN ^ 1 warning generated. compiling pg_connection.c pg_connection.c:679:17: warning: implicit conversion loses integer precision: 'long' to 'int' [-Wshorten-64-to-32] return INT2NUM(atol(port)); ~~~~~~~ ^~~~~~~~~~ 1 warning generated. compiling pg_copy_coder.c pg_copy_coder.c:228:15: warning: implicit conversion loses integer precision: 'long' to 'int' [-Wshorten-64-to-32] strlen = RSTRING_LEN(subint); ~ ^~~~~~~~~~~~~~~~~~~ /Users/apple/.rbenv/versions/3.0.2/include/ruby-3.0.0/ruby/internal/core/rstring.h:44:27: note: expanded from macro 'RSTRING_LEN' #define RSTRING_LEN RSTRING_LEN ^ pg_copy_coder.c:534:23: warning: implicit conversion loses integer precision: 'long' to 'int' [-Wshorten-64-to-32] input_len = end_ptr - start_ptr; ~ ~~~~~~~~^~~~~~~~~~~ 2 warnings generated. compiling pg_errors.c compiling pg_record_coder.c pg_record_coder.c:196:15: warning: implicit conversion loses integer precision: 'long' to 'int' [-Wshorten-64-to-32] strlen = RSTRING_LEN(subint); ~ ^~~~~~~~~~~~~~~~~~~ /Users/apple/.rbenv/versions/3.0.2/include/ruby-3.0.0/ruby/internal/core/rstring.h:44:27: note: expanded from macro 'RSTRING_LEN' #define RSTRING_LEN RSTRING_LEN ^ 1 warning generated. compiling pg_result.c pg_result.c:1006:17: warning: implicit conversion loses integer precision: 'long' to 'int' [-Wshorten-64-to-32] return INT2NUM(n); ~~~~~~~ ^ pg_result.c:1590:58: warning: 'rb_cData' is deprecated: by: rb_cObject. Will be removed in 3.1. [-Wdeprecated-declarations] rb_cPGresult = rb_define_class_under( rb_mPG, "Result", rb_cData ); ^ /Users/apple/.rbenv/versions/3.0.2/include/ruby-3.0.0/ruby/internal/core/rdata.h:173:18: note: expanded from macro 'rb_cData' #define rb_cData rb_cData() ^ /Users/apple/.rbenv/versions/3.0.2/include/ruby-3.0.0/ruby/internal/core/rdata.h:166:1: note: 'rb_cData' has been explicitly marked deprecated here RBIMPL_ATTR_DEPRECATED(("by: rb_cObject. Will be removed in 3.1.")) ^ /Users/apple/.rbenv/versions/3.0.2/include/ruby-3.0.0/ruby/internal/attr/deprecated.h:32:53: note: expanded from macro 'RBIMPL_ATTR_DEPRECATED' # define RBIMPL_ATTR_DEPRECATED(msg) __attribute__((__deprecated__ msg)) ^ 2 warnings generated. compiling pg_text_decoder.c compiling pg_text_encoder.c pg_text_encoder.c:194:14: warning: implicit conversion loses integer precision: 'long' to 'int' [-Wshorten-64-to-32] len = out - start; ~ ~~~~^~~~~~~ pg_text_encoder.c:406:15: warning: implicit conversion loses integer precision: 'long' to 'int' [-Wshorten-64-to-32] return optr - out; ~~~~~~ ~~~~~^~~~~ pg_text_encoder.c:410:12: warning: implicit conversion loses integer precision: 'long' to 'int' [-Wshorten-64-to-32] return 2 + RSTRING_LEN(*intermediate) * 2; ~~~~~~ ~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ pg_text_encoder.c:617:13: warning: implicit conversion loses integer precision: 'long' to 'int' [-Wshorten-64-to-32] nr_elems = RARRAY_LEN(value); ~ ^~~~~~~~~~~~~~~~~ /Users/apple/.rbenv/versions/3.0.2/include/ruby-3.0.0/ruby/internal/core/rarray.h:52:36: note: expanded from macro 'RARRAY_LEN' #define RARRAY_LEN rb_array_len ^ 4 warnings generated. compiling pg_tuple.c pg_tuple.c:482:15: warning: implicit conversion loses integer precision: 'long' to 'int' [-Wshorten-64-to-32] num_fields = RARRAY_LEN(values); ~ ^~~~~~~~~~~~~~~~~~ /Users/apple/.rbenv/versions/3.0.2/include/ruby-3.0.0/ruby/internal/core/rarray.h:52:36: note: expanded from macro 'RARRAY_LEN' #define RARRAY_LEN rb_array_len ^ 1 warning generated. compiling pg_type_map.c compiling pg_type_map_all_strings.c compiling pg_type_map_by_class.c compiling pg_type_map_by_column.c pg_type_map_by_column.c:161:52: warning: implicit conversion loses integer precision: 'long' to 'int' [-Wshorten-64-to-32] return dec_func( p_coder, RSTRING_PTR(field_str), RSTRING_LEN(field_str), 0, fieldno, enc_idx ); ~~~~~~~~ ^~~~~~~~~~~~~~~~~~~~~~ /Users/apple/.rbenv/versions/3.0.2/include/ruby-3.0.0/ruby/internal/core/rstring.h:44:27: note: expanded from macro 'RSTRING_LEN' #define RSTRING_LEN RSTRING_LEN ^ pg_type_map_by_column.c:230:17: warning: implicit conversion loses integer precision: 'long' to 'int' [-Wshorten-64-to-32] conv_ary_len = RARRAY_LEN(conv_ary); ~ ^~~~~~~~~~~~~~~~~~~~ /Users/apple/.rbenv/versions/3.0.2/include/ruby-3.0.0/ruby/internal/core/rarray.h:52:36: note: expanded from macro 'RARRAY_LEN' #define RARRAY_LEN rb_array_len ^ 2 warnings generated. compiling pg_type_map_by_mri_type.c compiling pg_type_map_by_oid.c compiling pg_type_map_in_ruby.c compiling pg_util.c pg_util.c:119:24: warning: implicit conversion loses integer precision: 'long' to 'int' [-Wshorten-64-to-32] return (char*)out_ptr - out; ~~~~~~ ~~~~~~~~~~~~~~~^~~~~ 1 warning generated. linking shared-object pg_ext.bundle ld: warning: argument missing after -rpath ld: file not found: dynamic_lookup clang: error: linker command failed with exit code 1 (use -v to see invocation) make: *** [pg_ext.bundle] Error 1 make failed, exit code 2 Gem files will remain installed in /Users/apple/workspace/runteq/muscle_beat/vendor/bundle/ruby/3.0.0/gems/pg-1.2.3 for inspection. Results logged to /Users/apple/workspace/runteq/muscle_beat/vendor/bundle/ruby/3.0.0/extensions/x86_64-darwin-20/3.0.0/pg-1.2.3/gem_make.out An error occurred while installing pg (1.2.3), and Bundler cannot continue. Make sure that `gem install pg -v '1.2.3' --source 'https://rubygems.org/'` succeeds before bundling. In Gemfile: pg   原因 結論から言うと、 pg_configのパスが指定されていなかったから! 解決方法 .bundle/configに以下を追記 BUNDLE_BUILD__PG: "--with-pg-config\\=/usr/local/bin/pg_config" その後、 bundle install で無事できました✌️
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む