Ask A Question

Notifications

You’re not receiving notifications from this thread.

How to use <script> in Rails 7

Clay Dennis asked in Javascript

How do I display a that holds a url with the .js I want to display. For example, <script src="https://greatsite.com/widgets/awesomescript.js">. I am very new to Rails, been learning for only 3 months.

Reply

You can have a controller respond to the JS format.

respond_to do |format|
  format.js
end

And then render a action.js.erb file

Reply

Is the action.js.erb file a partial of a view? Sorry, it's the first time I have seen a js.erb it has only been html.erb to this point.

Reply

Nope, partials start with an underscore. The js.erb extension is the format + the renderer. So this example is a response for the js format and rendered using erb.

Reply

Ok thanks I will give it a try.

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 82,329+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more.

    © 2024 GoRails, LLC. All rights reserved.