I have been following a Rails Tutorial on Building a Link Shortener with Rails 6 and Ruby 2.xx to create an app. However, I am using Rails 7.0.4 and Ruby 3.0.0. I encountered an issue with my create.js.erb file not functioning properly. After some research on Google, I found out that .js.erb has been removed from Rails 7 and replaced with turbo_stream and hot stimulus.js.
My question is how can I integrate my code from create.js.erb, which contains embedded Ruby in JavaScript, into the new Rails 7 setup, specifically the links_controller.js located in the javascript >> controller directory, as it is purely based on JavaScript now?
Here is the content of create.js.erb:
var lookupCode = "<%#= @link.lookup_code %>";
var element = document.getElementById('short-link');
element.innerHTML = lookupCode;
To address this, I created a new file named links_controller.js under the javascript >> controller directory with the following content:
import { Controller } from '@hotwired/stimulus';
export default class extends Controller {
connect() {
this.element.textContent = "I believe I should hook the content of create.js.erb here";
}
}
Any assistance on this matter would be highly appreciated. I have diligently researched and searched online before posting this question. Unfortunately, the only relevant source that could potentially answer my query has been closed:
Ruby on Rails 7 - Is there any way to implement js.erb files?