LoginSignup
1
0

More than 5 years have passed since last update.

jbuilderのjsonにSTIのtypeカラムを含める

Posted at

はじめに

railsからreactにjsonを渡す時にそのままではSTIのtypeが渡せずにfrontでSTIの判定ができなかった。
例として、blockモデルがnormal_blockやplain_blockなどのSTIを持っているとします。

json.lp_blocks @lp_blocks.all do |lp_block|
  json.lp_block lp_block # これだとrailsの使用上typeはjsonに含まれない。
  ...
end

解決策

こちらの記事のコメントを参考にしました。

json.lp_blocks @lp_blocks.all do |lp_block|
  json.lp_block lp_block.as_json(methods: :type)
  ...
end
1
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
1
0