1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-16 21:48:02 +00:00

Compare commits

...

3 Commits

Author SHA1 Message Date
jbranchaud
bf04dfcca5 Add Rollback A Couple Migrations as a Rails TIL 2024-10-09 17:44:45 -05:00
jbranchaud
24b1b02d52 Add Define The Root Path For The App as a Rails TIL 2024-10-09 13:23:09 -05:00
jbranchaud
8ef2cfdc69 Add Prevent erb_lint From Removing Opening Tags as a Ruby TIL 2024-10-09 10:53:39 -05:00
4 changed files with 111 additions and 1 deletions

View File

@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
_1455 TILs and counting..._
_1458 TILs and counting..._
---
@@ -899,6 +899,7 @@ _1455 TILs and counting..._
- [Custom Validation Message](rails/custom-validation-message.md)
- [Customize Paths And Helpers For Devise Routes](rails/customize-paths-and-helpers-for-devise-routes.md)
- [Customize The Path Of A Resource Route](rails/customize-the-path-of-a-resource-route.md)
- [Define The Root Path For The App](rails/define-the-root-path-for-the-app.md)
- [Delete Paranoid Records](rails/delete-paranoid-records.md)
- [Demodulize A Class Name](rails/demodulize-a-class-name.md)
- [Different Ways To Add A Foreign Key Reference](rails/different-ways-to-add-a-foreign-key-reference.md)
@@ -964,6 +965,7 @@ _1455 TILs and counting..._
- [Rescue From With A Separate Method](rails/rescue-from-with-a-separate-method.md)
- [Respond With JSON Regardless of Content Type](rails/respond-with-json-regardless-of-content-type.md)
- [Retrieve An Object If It Exists](rails/retrieve-an-object-if-it-exists.md)
- [Rollback A Couple Migrations](rails/rollback-a-couple-migrations.md)
- [Rollback A Specific Migration Out Of Order](rails/rollback-a-specific-migration-out-of-order.md)
- [Rounding Numbers With Precision](rails/rounding-numbers-with-precision.md)
- [Run A Rake Task Programmatically](rails/run-a-rake-task-programmatically.md)
@@ -1212,6 +1214,7 @@ _1455 TILs and counting..._
- [Pattern Match Values From A Hash](ruby/pattern-match-values-from-a-hash.md)
- [Percent Notation](ruby/percent-notation.md)
- [Precedence Of Logical Operators](ruby/precedence-of-logical-operators.md)
- [Prevent erb_lint From Removing Opening Tags](ruby/prevent-erb-lint-from-removing-opening-tags.md)
- [Print Data To Formatted Table](ruby/print-data-to-formatted-table.md)
- [Question Mark Operator](ruby/question-mark-operator.md)
- [Rake Only Lists Tasks With Descriptions](ruby/rake-only-lists-tasks-with-descriptions.md)

View File

@@ -0,0 +1,37 @@
# Define The Root Path For The App
The `root_path` helper that you might want to use in Rails controllers and
views is not available by default.
```ruby
> Rails.application.routes.url_helpers.root_path
ruby/3.2.2/lib/ruby/gems/3.2.0/gems/irb-1.14.0/lib/irb.rb:1285:in `full_message': undefined method `root_path' for #<Module:0x0000000106d11738> (NoMethodError)
Rails.application.routes.url_helpers.root_path
^^^^^^^^^^
Did you mean? logout_path
book_path
```
It needs to be declared in the `config/routes.rb` file with the controller
action that it points to.
```ruby
# config/routes.rb
Rails.application.routes.draw do
root 'home#index'
end
```
Once this is defined the `root_path` will now be available with the rest of
your URL helpers.
```ruby
better-reads(dev)> reload!
Reloading...
better-reads(dev)> Rails.application.routes.url_helpers.root_path
=> "/"
```
[source](https://guides.rubyonrails.org/routing.html#using-root)

View File

@@ -0,0 +1,25 @@
# Rollback A Couple Migrations
Let's say we need to rollback a couple Rails migrations that have been applied
to our local environment. We run `rails db:migrate:status` and see that there
are _2_ migrations that we want to _undo_.
We can accomplish this by using the `STEP` env var with the rollback command.
```bash
$ rails db:rollback STEP=2
```
Just set `STEP` to the number of migrations that we need to rollback. If we
then rerun `rails db:migrate:status` we'll now see those latest two migrations
are `down`.
Note: by default Rails doesn't like to operate with pending migrations. If we
want to temporarily disable the pending migration check, we can alter the
migration error config in `config/development.rb`.
```diff
# Raise an error on page load if there are pending migrations.
- # config.active_record.migration_error = :page_load
+ config.active_record.migration_error = false
```

View File

@@ -0,0 +1,45 @@
# Prevent erb_lint From Removing Opening Tags
The [`erb_lint` gem](https://github.com/Shopify/erb_lint) is a tool from
shopify for linting and auto-formatting ERB files. When I first set it up in a
Rails codebase with the base `.erb-lint.yml` recommended in the README, I ran
into a pernicious issue. The linter wanted to remove opening tags (i.e. `<%`
and `<%=`) from my ERB files.
So, for a file that looked like this:
```erb
<div>
<%= form_with(url: login_path, scope: :session) do |f| %>
<div>
<%= f.label :email %>
<%= f.email_field :email %>
</div>
</div>
```
It would get formatted to this:
```erb
<div>
form_with(url: login_path, scope: :session) do |f| %>
<div>
f.label :email %>
f.email_field :email %>
</div>
</div>
```
Yikes!
I had to disable a couple rules (under `rubocop_config:`) in the `.erb-lint.yml` file to get it to stop
doing this.
```yaml
Layout/InitialIndentation:
Enabled: false
Layout/TrailingEmptyLines:
Enabled: false
```
[source](https://github.com/Shopify/erb_lint/issues/222)