Retrieving Link Metadata with Background Jobs Discussion
Posting here in case anyone else ran into the issue where they tried to run a test, but got the error of having pending database migrations (also after running db:migrate).
For me what helped is running db:migrate for the test environment, so:
bin/rails db:migrate RAILS_ENV=test
After that, tests would run fine.
I'm running Rails 8 and the respond_to links controller portion of the code is not needed for the immediate update on the frontend from the metadata job—link.broadcast_replace_to(link) works without any additional configuration. Does this have something to do with Hotwire or something specific in Rails 8?
Additionally, the clearing of the form field is also automatically cleared once a link is save. Is this also a newly baked in feature of Hotwire?
Another great episode, thanks! :)
For anyone else exploring Stimulus a bit more, an alternative to re-focusing the input element after submitting the form would be to use a static target in the Stimulus controller. IMO it feels more intuitive to clearly specify the target when we define the controller, rather than find it later by some other attribute like its autofocus property.
# reset_form_controller.js
export default class extends Controller {
static targets = ["input"] # define the target here
reset() {
this.element.reset()
this.inputTarget.focus() # use the target here
}
}
# links/_form.html/erb
<%= form_with(model: link, ...[truncated]... }) do |form| %>
<%= form.url_field(:url, ..., data: { reset_form_target: "input" }) %> # link the HTML element to the controller target here
<%= form.submit "Save", ... %>
<% end %>