attempting to embed a .js.erb file within a webpage

Struggling to incorporate js.erb script into my view for a polling-based chat feature. What seemed straightforward at first has turned into a complex task.

Snippet of the js.erb script:

$( document ).ready(function() {   

    $(function() {
        setTimeout(updateMessages, 1000);
    });

    function updateMessages () {
        var game_id = $("#game_chat").attr("data-id");
        $("#messages").html("<%=escape_javascript(render(:partial => 'messages')) %> ");
        setTimeout(updateMessages, 1000);
    }

});

The corresponding partial code is simple:

<%= render @messages %>

Encountering difficulties in linking .js.erb code within the view. Attempted using this approach without success:

<%= javascript_include_tag  "games/messages.js.erb" %>

Answer №1

When utilizing the asset pipeline, there is no need for .js.erb files. By using <%= javascript_include_tag "games/messages" %>, Tilt (the engine responsible for selecting the template engine) will prioritize the .js file over everything else, making it the only thing visible to the browser and view. However, incorporating a large string like this can lead to potential security risks, as it may trigger an eval on your code. It's crucial to trust the source completely to avoid any vulnerabilities such as xss.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The utcParse() function in D3.js might return a null value

I am struggling to parse a date format that appears as follows: 2017-02-18T09:00:00+06:00. Currently, I am attempting to use the following format for parsing: d3.utcParse("%Y-%m-%dT%H:%M:%S+%Z");, however, it is returning null. Do you have any suggesti ...

Issue encountered while attempting to attach an event listener to an element within a class

Upon running the code, I encountered an error message that reads: Uncaught TypeError: this.startButton.addEventListener is not a function and I'm unsure of how to resolve it. Although I can successfully console.log the button inside the class, adding ...

What is the correct way to generate an await expression by utilizing recast/esprima?

I have an issue with a JavaScript function export const cleanUp = async () => { await User.destroy({ where: {} }); }; I am attempting to add a line below await User.destroy({ where: {} }) using recast.parse(`await ${module}.destroy({ where: {} } ...

Double invocation of useEffect causing issues in a TypeScript app built with Next.js

My useEffect function is set up with brackets as shown below: useEffect(() => { console.log('hello') getTransactions() }, []) Surprisingly, when I run my app, it logs "hello" twice in the console. Any thoughts on why this might be ...

Error encountered in Storybook: The value is not iterable (property Symbol(Symbol.iterator) cannot be read)

I recently created a React library using and opted for the React + TypeScript + Storybook template. You can find the entire codebase here → https://github.com/deadcoder0904/react-typical I encountered the following error: undefined is not iterable ( ...

User has logged out, triggering the browser tab to close automatically

Is it possible to automatically log out the user when all browser tabs are closed? How can I obtain a unique ID for each browser tab in order to store it in local storage upon opening and remove it upon closing? Here is what I have attempted: I am utiliz ...

I am having trouble comprehending this JavaScript code, could someone provide me with assistance?

Currently delving into the world of JavaScript functions and stumbled upon this code snippet with the following output: Output: "buy 3 bottles of milk" "Hello master, here is your 0 change" function getMilk(money, costPerBottle) { ...

Image created from BitmapData stroke

Having trouble creating a sprite from BitmapData? It seems that when working with a rectangular shape, everything runs smoothly. However, when dealing with just a line, the sprite ends up empty. bmd = this.game.add.bitmapData(this.line.width, this.line. ...

Attempting to direct Heroku towards the index.html file

I am currently using Heroku for deploying my website with node.js. I noticed that in the index.js file, the tutorial has "Hello World" in the response.end method, but I would like it to display my index.html file instead. Is there a way to achieve this? ...

Caching JSON Results in ASP .Net MVC 5

Can someone help me understand how to implement caching for JsonResult actions in an MVC 5 application? I am looking to cache certain actions that are called via AJAX using the [OutputCache()] attribute. Some of these actions return an ActionResult with HT ...

AngularJS: Unable to locate element using getElementById function

Using a third party JavaScript application, I have integrated and invoked a function within one of my AngularJS factories. This function requires the id of a DOM element as a parameter, as it manipulates this element. ThirdPartyApp.doSomething("#elementId ...

The DataType.MultilineText field in my modal popup partial view is causing all validation to fail

I encountered a peculiar issue recently. After creating a new asp.net mvc5 application, I decided to upgrade it to asp.net mvc-5.2.2. The problem arose with the following field:- [Required] [StringLength(200)] public string Name { get; set; } When ren ...

Developing a react native library (create-react-native-library) incorporating a distinct react-native version within its designated Example directory

I'm looking to develop a React Native library, but the testing folder (example folder) it contains the latest version of React Native. However, I specifically need version 0.72.6 in the example folder. Is there a command for this? Current command: np ...

Using json_encode on an array after applying array_filter yields a unique outcome

My goal is to have json_encode output an array format like this: [{key: "value"},{key:"value"},...] But instead, the current output looks like this: {"1": {key: "value"}, "2": {key: "value"}, ...} Everything was working fine until I implemented an arra ...

Does the AJAX request originate from my website, or is it from Zend Framework?

I'm facing a situation where I have a URL (controller/action) that is accessed through AJAX requests. The concern here is that since AJAX calls are client-side, there's a risk of other websites copying my JavaScript code and accessing the same UR ...

selecting arrays within arrays according to their date values

With an array of 273 arrays, each containing data about a regular season NFL football game, I am looking to categorize the games by week. In total, there are 17 weeks in the NFL season that I want to represent using separate arrays. The format of my array ...

Is a Page Refresh Required to Reload Routes in ReactJS?

I have created menu links labeled Home, About, What I Do, and Experience for my portfolio site using BrowserRouter. However, when I click on these links, the components do not load properly on the page; they only render correctly after a page refresh. This ...

No matter the method used for sending, the request body from post always returns as undefined

I have encountered a problem where none of the existing answers have helped me resolve it. I am attempting to pass a variable through ajax in the following manner: var myData = "Hi Og"; $.ajax({ type: 'POST', data: myData, url: &a ...

Ways to swap webpack-dev-server for alternative servers such as express or apache

After using vue-cli to scaffold a Vue app, I am looking to set up a new web server instead of webpack-dev-server. I am interested in having a separate configuration file to customize the server settings, similar to using Node Express for production deploym ...

Is it possible to extract form data from a div tag based on its class name?

I'm working with some code that looks like this: var iconContainer = document.getElementById('iconContainer'); var icon = iconContainer.getElementsByClassName("item"); for (var i = 0; i < icon.length; i++) { icon[i].addEventListener ...