CSRF対策の実装について

railsだとCSRF対策でviewにcsrf_meta_tagsヘルパーを使って防ぎますが

一々viewに書き込むのが面倒なので普通レイアウトファイルに書き込みます。

これの実装の定義のコードが

class ApplicationController < ActionController::Base
protect_from_forgery with: :exception

です。

ソースの場所は

app/controllers/application_controller.rb

です。

詳しい書き方はrailsチュートリアル

https://railstutorial.jp/chapters/static_pages?version=5.1#code-application_layout

ログに特定のパラメータの値の記録を残さないために

# Be sure to restart your server when you modify this file.

# Configure sensitive parameters which will be filtered from the log file.
Rails.application.config.filter_parameters += [:password]
 
以前ログを盗まれるという話がちらほらありましたよね
railsの話ですがログにパラメータの記録が残ってしまうので
Rails.application.config.filter_parametersにログに残したくないパラメータを追記しましょう。この例ではパラメータの:passwordをログから除外しています。パスワードの入力ログの記録を残さない方法です。パスワードを残さないのはデフォルトの実装です。
ファイルの場所は
config/initializers/filter_parameter_logging.rb
にあります。

RailsやPHPのAPIの参照について

一つのAPIを作るにあたってreturn json形式で値を返すと思うのですがじゃあそのjsonをどう取り込むかを記述します。PHPであればGuzzleHttpを使っています。

RailsであればNet::HTTPを使うでしょうか?

ちなみにpaizaのチャプターではcurlを使っていましたね。

細かい記述については他の方の記事を参照していただくとしてjsonを返すAPIをどうやって取り込むかご参考までに。

rails5の名詞の命名規則の例

railsのモデルやコントローラの命名規則の複数形や単数形の例を見てみる

config/initializers/inflections.rbに例をコメントアウトして持っている。

# Be sure to restart your server when you modify this file.

# Add new inflection rules using the following format. Inflections
# are locale specific, and you may define rules for as many different
# locales as you wish. All of these examples are active by default:
ActiveSupport::Inflector.inflections(:en) do |inflect|
#   inflect.plural /^(ox)$/i, '\1en'
#   inflect.singular /^(ox)en/i, '\1'
#   inflect.irregular 'person', 'people'
#   inflect.uncountable %w( fish sheep )
# end

# These inflection rules are supported but not enabled by default:
ActiveSupport::Inflector.inflections(:en) do |inflect|
#   inflect.acronym 'RESTful'
# end
 
 例えば雄牛である、oxを見てみるとは正規表現で^が先頭を示していて$が最後尾を示している。
のでズバリoxを抜き出しており ()で囲んだところを\1で参照できる。()が増えれば\2などのように順に参照できる。最後のiはフラグと言ってiの場合は大文字小文字を区別しないでパターンマッチする。つまりoxの複数形はoxenでありoxenの単数形はoxである。
イレギュラーに変化する場合の例はpersonがpeopleになると表記しているのはお分かりだろう。
 
uncountableは複数形と単数形が変化しないものを設定できる。
上記例で言えばfishとsheepなどは単数形複数形で変化しないと設定されている。
 

[1] pry(main)> ActiveSupport::Inflector.pluralize "project"

=> "projects"

[2] pry(main)> "project".pluralize

=> "projects"

[3] pry(main)> "projects".singularize

=> "project"

[4] pry(main)> "fish".pluralize

=> "fish"

[5] pry(main)> "ox".pluralize

=> "oxen"

[6] pry(main)> "person".pluralize

=> "people"

 

等々rails consoleで確認してほしい。

また任意のイレギュラーな変化は

inflect.irregular "単数形","複数形"

で指定できる。