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.
Use `requestSubmit` for form submits with Turbo
Turbo listens to submit events on form. Hence, always use requestSubmit to submit a form using JS.
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.
Rails 6, Ruby 3, Bootstrap 5 and Hotwire
Extending the blogpost from Bootrails with Hotwire: All the fancy new things (as of when I'm writing this TIL):
Rails Cache-Control
For enabling setting cache-control headers of static assets and using a CDN on Heroku:
Fix input on Heroku's rails console
If you end up pasting lots of lines into a rails console on Heroku (or else where), you might get issues with the lines "bleeding" into each other.
Unique ID for DOM in Rails
Rails has many great view helpers, one of which is dom\id:*
Add new types to Rails 5 attributes API
The Rails 5 attributes API allows us to build form objects very easily.
Attaching file to ActiveStorage without HTTP
If you want to attach a file you generated on disk or downloaded from a user-submitted URL, you can do it like this.
Create a Hash from an Enumerable (Rails 6.0)
New method allowing you to create a Hash from an Enumerable:
Create idempotent migrations
When you want to write destructive migrations please use if\exists: true when you’re trying to remove a table and check for a table existence when you want to add or remove a column…
How to find god objects in your project (Rails)
Go to your project root and cd into app/models (using CLI obviously) then run wc -lw \ | sort -u*
HTML forms that submit to another action
I've known for a long time that you can submit values with submit buttons so you can track which button was used to submit the form e.g.
If you want to override previously set order
If you want to override previously set order (even through default\_scope), use reorder() instead.
Limited has_many associations
Limited has_many associations
List your model names
NOTE: RUBYOPT=-W0 avoids verbosity when running rails command which suppresses warnnings.
Postgres Functions & Non-sargable Queries
Using postgres functions inside a where clause can make a query non-sargable.
PostgreSQL UPDATE Join
This query shows how to use the PostgreSQL UPDATE join syntax to update data in a table based on values in another table. for example;
Scope or scope in Rails
scope :human, -> { user.or(author) } scope :terminator, -> { admin.or(robot) } `
Show TODO and other notes in your Rails app
Did you know you can show all TODOs/OPTIMIZE/FIXME in your rails app with rails notes?
Tag rails logs with useful information
Just learned about tagged logging in rails. Did you know you can tag a log by using
Use minItems/maxItems in collection JSON schemas
To make your collection JSON schemas more reilable use minItems and maxItems so that you can trust your API.