24
5

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.

三項演算子の処理なし時の注意点

24
Last updated at Posted at 2022-05-22

どうもaono1234と申します。記事がいいなと思ったらtwitterのフォローもお待ちしております‼
https://twitter.com/takeshi_program

三項演算子でバグが発生して1hぐらいハマったので覚書として記載します。

状況

  • html上にrubyを出力しようとしていた。
  • 三項演算で代入した変数をpタグで囲っているのに、検証ツールで見るとpタグで囲えてない(下記の状況)
  • 三項演算子でやりたかったこと→@noticeに値が入っていたら「notice = @notice」で変数に代入、@noticeに値が入ってない場合は処理なし
    Inkedバグ_LI.jpg

バグの原因

  • 三項演算子の右側を空欄にしていたからだった。
  • ダミー処理として()を記載したら治った。
    Inked正常_LI.jpg

考察

  • 三項演算子の右側を空欄にしたことで、1行目と2行目のコードが合体し「@notice ? notice = @notice : notice」となっていたのだと推測
  • 2行目の処理<% notice %>が1行目に吸収されたことで2行目にはpタグだけが残った。
24
5
1

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
24
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?