OOZOU
ติดต่อเรา
Back to TIL
ruby on rails

Add new types to Rails 5 attributes API

13 ตุลาคม 2563

The Rails 5 attributes API allows us to build form objects very easily.

I had previously been using an after_initialize callback to downcase emails, with the following code I can define an :email date type and values are automatically cast when set.

This has the advantage of casting subsequent calls to set the email value.

class EmailType < ActiveModel::Type::String
  def cast(value)
    return super unless value.is_a?(String)
    super(value.downcase)
  end
end

ActiveModel::Type.register(:email, EmailType)
class Foo
  include ActiveModel::Model
  include ActiveModel::Attributes

  attribute :email, :email
end
> Foo.new(email: 'EMAIL@MAIL.com').email
=> 'email@mail.com'

More on ruby on rails

ruby on rails10 กุมภาพันธ์ 2565

Use Pundit Policies to manage permitted parameters, defaults and per action

To manage permitted params you can add permitted\attributes and permitted\attributes\for\#{action} in your policies e.g.

ruby on rails31 มกราคม 2565

Use `requestSubmit` for form submits with Turbo

Turbo listens to submit events on form.  Hence, always use requestSubmit to submit  a form using JS.

ruby on rails19 มกราคม 2565

Ignore bundled gems for Rubocop and ESLint in GitHub Actions

Rails projects will often install gems into the vendor directory in GitHub actions so we can cache them for the consecutive runs.

มีโปรเจกต์ในใจ?

เรายินดีรับฟังสิ่งที่คุณกำลังสร้าง

เริ่มต้นสนทนาhello@oozou.com
OOZOU Logo

กรุงเทพฯ · สิงคโปร์ · ฮ่องกง

X
Facebook
Instagram
LinkedIn

บริการ

  • พัฒนาเว็บ
  • AI และ Generative AI
  • พัฒนาแอปมือถือ
  • การวิเคราะห์และวิศวกรรมข้อมูล
  • ออกแบบ UI/UX และผลิตภัณฑ์
  • การเปลี่ยนแปลงทางดิจิทัล

บริษัท

  • เกี่ยวกับเรา
  • ร่วมงานกับเรา
  • บล็อก
  • เอกสารวิชาการ
  • กรณีศึกษา
  • Today I Learned
  • ติดต่อเรา

เพิ่มเติม

  • อุตสาหกรรม
  • ขอใบเสนอราคา
  • เครือข่ายพันธมิตร
© 2026 OOZOU สงวนลิขสิทธิ์
นโยบายความเป็นส่วนตัวจรรยาบรรณนโยบาย ABAC