0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Rails】変数やパラメータのnullチェック。例外処理の実装方法

Posted at

パラメータや変数を他の場所から受け取って処理をする場合は、冒頭でデータが渡されているかのチェック(nullチェック)を行う。

データに入力値がないとエラーになる処理がある場合は必ず記述する。

nullチェックの方法

if 変数.blank?
 raise "エラーメッセージ"
end

**実例** nullチェック対象の変数が複数ある場合は`||`でつなげる。
def test
  id = params[:id]
  name = params[:name]
  
  if id.blnak? || name.blank?
    raise "id or name missing"
  end
  
  処理

end

▼ブラウザの表示
image.png
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?