[Rails] Assigning variables from controller to JavaScript

Controller side

controller.rb


def show
    @location = Location.find(params[:id])
  end

view side

<script>
  {
    const num1 = <%= @location.num1 %>   <%#Substitution%>
    const num2 = <%= @location.num2 %>   <%#Substitution%>


    var MyLatLng = new google.maps.LatLng(num1, num2);
    var Options = {
      zoom: 15, //Map scale value
      center: MyLatLng, //Map center coordinates
      mapTypeId: 'roadmap' //Map type
    };
    var map = new google.maps.Map(document.getElementById('map'), Options);
  }
</script>

Recommended Posts

[Rails] Assigning variables from controller to JavaScript
Replace Rails favorite feature (Ajax) from jQuery to plain JavaScript
[Rails 5] How to use gem gon ~ How to pass variables from Rails to JS ~
The road from JavaScript to Java
How to convert param to hash with Rails controller (updated from time to time)
From pulling docker-image of rails to launching
[Rails] How to convert from erb to haml
Transition from Struts2 to Spring MVC (Controller)
[Rails] I tried to raise the Rails version from 5.0 to 5.2
How to link Rails6 Vue (from environment construction)
Rails Tutorial Chapter 1 From Zero to Deployment [Try]
How to separate .scss by controller in Rails
I want to play with Firestore from Rails
[Rails] Introducing pay.jp (from view customization to registration)
Introduce dotenv to Docker + Rails to manage environment variables
About Rails controller
How to embed JavaScript variables in HTML with Thymeleaf
Pass Form class from Controller to Thymeleaf by Spring-Boot
Use Extend (Concerns) in Rails to standardize Controller processing.
[Updated from time to time] Ruby on Rails Convenient methods
[Rails] Save information from one form to multiple tables
How to download images from AWS S3 (rails, carrierwave)
Rails Tutorial 4th Edition: Chapter 1 From Zero to Deployment
[Rails] How to edit and customize devise view and controller
[Ruby on Rails] From MySQL construction to database change
[Rails] How to load JavaScript in a specific view
Changes from Java 8 to Java 11
Sum from Java_1 to 100
Migrate from JUnit 4 to JUnit 5
Introducing CircleCI to Rails
Introducing Bootstrap to Rails 5
Introducing Bootstrap to Rails !!
Introduce Vue.js to Rails
How to uninstall Rails
From Java to Ruby !!
Cloud9 (Rails) from Github
How to set environment variables when using Payjp with Rails
Chewing Rails Tutorial [Chapter 1 From Zero to Deployment] Second Half
How to write a migration from Rails datetime type to date type
Back to top button made only with Javascript (rails, haml)
From Ruby on Rails error message display to Japanese localization
[Rails] Function to search and list products from multi-level categories
How to set and describe environment variables using Rails zsh
[Rails] Integration test using Capybara (from introduction to simple test execution)