DEV Community

thomasvanholder
thomasvanholder

Posted on • Updated on

How to turn on Rails 7 framework defaults (part 1)

This tutorial inspects turning on each setting of new_framework_defaults_7.0.rb file. The intended goal is to fully migrate to Rails 7's default set-up.

  1. button_to_generates_button_tag
  2. apply_stylesheet_media_default
  3. verify_foreign_keys_for_fixtures

1. button_to_generates_button_tag

Rails.application.config.action_view.button_to_generates_button_tag = true
Enter fullscreen mode Exit fullscreen mode

What

The button_to helper had inconsistent behaviour. Rails PR.

  • string argument: <input type="submit">
  • block argument: <button type="submit">
# 'new' is the string argument
<%= button_to "New", new_task_path %>
Enter fullscreen mode Exit fullscreen mode

Before

<input type="submit" value="New">
Enter fullscreen mode Exit fullscreen mode

After

<button type="submit">New</button>
Enter fullscreen mode Exit fullscreen mode

2. apply_stylesheet_media_default

Rails.application.config.action_view.apply_stylesheet_media_default = false
Enter fullscreen mode Exit fullscreen mode

What

A stylesheet_link_tag can specify a media type. When omitting a media attribute, screen is no longer set as the default media value Rails Guide.

<%= stylesheet_link_tag "application" %>
Enter fullscreen mode Exit fullscreen mode

Before

<link rel="stylesheet" href="...css" media="screen">
Enter fullscreen mode Exit fullscreen mode

After

<link rel="stylesheet" href="...css">
Enter fullscreen mode Exit fullscreen mode

3. verify_foreign_keys_for_fixtures

Rails.application.config.active_record.verify_foreign_keys_for_fixtures = true
Enter fullscreen mode Exit fullscreen mode

What

Ensures all foreign key constraints are valid after fixtures are loaded in tests. Rails PR

Before

# test/fixtures/parrots.yml
george:
  name: "Curious George"
  pirate: redbeard

# test/fixtures/pirates.yml
blackbeard:
  name: "Blackbeard"
Enter fullscreen mode Exit fullscreen mode

Fixtures can refer to a missing association.
parrots(:george).pirate will be nil

After

An error is raised when fixtures are loaded in tests.
"Foreign key violations found in your fixture data. Ensure you aren't referring to labels that don't exist on associations."

Discussion (5)

Collapse
briankephart profile image
Brian Kephart

Any tips on how to get info on which fixture has the violation?

Collapse
sturpin profile image
Sergio Turpín

Did you find a solution? 🤦‍♂️

Collapse
briankephart profile image
Brian Kephart

I used VS Code to open the local copy of Active Record and edited out a statement that was suppressing the backtrace. Then I could see where the errors were coming from.

Thread Thread
sturpin profile image
Sergio Turpín

Fantastic!! Do you remember exactly what you changed?

Thread Thread
briankephart profile image
Brian Kephart

No, but I think it was a statement in the AR Postgres adapter that rescues a PG exception and returns false instead, which then causes AR to raise the fixture error.