By changing the name of my file to .js.erb, I am able to seamlessly incorporate erb syntax within my javascript code, but this

Currently, I am using chessboard-0.3.0.js in a rails application. Due to the rails asset pipeline setup, I've had to make adjustments to the js code specifically related to rendering images, such as Pieces. In line 445 of chessboard.js, it states:

cfg.pieceTheme =  'img/chesspieces/wikipedia/{piece}.png'

This posed an issue for rails, so I renamed chessboard with the .erb extension and modified that line to:

cfg.pieceTheme =  <%= asset_path('chesspieces/wikipedia/{piece}.png') %> 

After placing all the required images in the assets/images folder.

The problem lies in the fact that the images are still not being rendered. My assumption is that the placement of {piece}.png in that line might be causing the issue since it's essentially js inside a rails helper, correct? This could be why my images aren't showing up. Going through the entire 'almost 2000' lines of code to individually access each image seems daunting, especially as a beginner, as it's easy to inadvertently cause more errors along the way.

Is there a solution to this predicament without having to extensively rewrite the code?

Answer №1

If you're looking for an alternative solution, it may involve some javascript code rewriting.

The issue with the current code not working is due to the fact that the erb file is compiled before rendering, while the javascript is executed after rendering. This causes the asset_path to be incorrect and static instead of dynamic.

One approach could be to create an object containing all available routes and then use it to dynamically insert path names.

For example, if your loop looks something like this:

var piecesList = [
  "first",
  "second"
]
$.each(var piece in piecesList) function(piece) {
  var image_path = "img/path/{piece}.png"
}

You can manually assign the pieces to paths in an object like this:

var piecesRoutes = {
     "first" : "<%= image_path('chesspieces/first.png') %>",
     "second" : "<%= image_path('chesspieces/second.png') %>",
   }

Then, within the loop, retrieve the path like so:

var image_path = piecesRoutes[piece]

Answer №2

If you want to avoid hashing your chess pieces, consider including them directly in the /public directory. Since they likely won't change if they are from an external library, caching shouldn't be a concern.

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

Unusual hue in the backdrop of a daisyui modal

https://i.stack.imgur.com/fLQdY.png I have tried various methods, but I am unable to get rid of the strange color in the background of the Modal. Just for reference, I am using Tailwind CSS with Daisy UI on Next.JS. <> <button className='btn ...

Tracking your daily nutrition using cronometer with the powerful combination of JavaScript

I'm currently developing a JavaScript cronometer in vueJS. I want the timer to start at 5 minutes (300 seconds) and then continue counting minutes and seconds. Although my cronometer is functioning, I am struggling to get it to start counting minutes ...

Tips for selecting an <select> option value based on an URL parameter automatically

I have a 2-step form where I am successfully passing the first name in the URL from step 1 to step 2, but I am struggling to do the same for a select field. Here's an example of what I have: In the URL: ?firstname=Bob Form Field: <input type= ...

how to protect your javascript and php code

I have a question about "injection", specifically javascript injection which I find confusing. Suppose I have a javascript function that sends an AJAX request to get a user's permission level, and then uses an if statement to assign power based on th ...

The function this.$set is failing to update an array in VueJS

I am facing an issue where the console log shows me the updated array xyz, but when I try to print it in the DOM using {{xyz}}, it does not update. Can anyone shed some light on why this might be happening? data() { return { xyz: [] } }, met ...

Type inference in TypeScript with transitivity

Consider this code snippet for illustration: function foo(t: "number"): number function foo(t: "string"): string function foo(t: "boolean"): boolean function foo(t: "number" | "string ...

There seems to be an issue with the import class for React PropTypes. The prop

I have multiple oversized items that are utilized in numerous components, so I created a PropTypes file for each item. For example: PropTypes/PropLargeObject.js This file contains: import PropTypes from "prop-types"; const PropLargeObject = Prop ...

Deregister channel listener

My application has a functionality where users can subscribe to Twilio chat channels. When a user clicks on a channel, the chat opens up, messages are loaded, and the user is subscribed to receive new messages using this.state.channel.on('messageAdded ...

Examine the spans and delete the first-child node

Basically, this functionality allows the user to cycle through hidden information by clicking on it. Here is how you can structure your HTML: <div id="facts"> <span>click to cycle</span> <span>fact 1</span> <s ...

What is the reason behind the browser crashing when a scrollbar pseudo-class is dynamically added to an iframe?

1. Insert a new iframe into your HTML: <iframe id="iframe-box" onload=onloadcss(this) src="..." style="width: 100%; border: medium none; "></iframe> 2. Incorporate the following JavaScript code into your HTML file ...

Adding substantial sections of HTML without any adjustments

While I am working on DOM manipulation, I have encountered the need to insert large blocks of HTML. I am looking for a more efficient way to do this rather than relying on concatenation or creating a messy code structure. Consider the simple code snippet b ...

Executing a javascript function numerous times

Here are a few examples: <div class="statement">text goes here</div> <div class="response"><input type="text" id="amount1" style="border: 0; color: #f6931f; font-weight: bold;" /></div> <div id="sli ...

React JS Calendar

I'm currently working on a project where I need to create a calendar component using React. The design requires the days to be displayed like in the Windows calendar, starting from Sunday even if it's another month and ending with Saturday. The ...

Getting URL Parameters in Angular JS

How should one go about retrieving URL parameters in Angular? For instance, consider this URL: http://example.com/mypage.html?product=1234®ion=4&lang=en Thank you ...

Determine the size of a BSON object before saving it to a MongoDB database using

I am currently in the process of determining the best way to calculate the size before storing certain data in MongoDB. After writing a script that parses and combines data into a single document, I have encountered an error when trying to use instance.sav ...

"Assure that setTimeout will be executed within the then method in

Within an Immediately Invoked Function Expression (IFFE), I am returning a Promise. The first thing that is logged is first, followed by third, and then second. Despite having the setTimeout inside the then block, shouldn't everything within it be exe ...

Encountered an issue while setting up ng-circle-progress in Angular: Unable to find an exported member in

I am currently working on incorporating the ng-circle-progress functionality by referring to the documentation at https://www.npmjs.com/package/ng-circle-progress. Here is a snippet from the .ts file: import { Component } from '@angular/core'; i ...

How can a non-commonJS library be effectively integrated into the Webpack environment?

Looking to incorporate an external and non-commonJS library to define an AngularJS module. What is the best approach since directly importing it won't work, like this: import MyLibrary from 'MyLibraryPath' angular.module('MyApp' ...

What is causing the error that app.js file cannot be located?

Here is the layout of my directory: ReactCourse // Main folder public // Subfolder within ReactCourse index.html // HTML file with linked js file app.js // JavaScript file This is the content of index.html: <!DOCTYPE ...

Encountering an error stating "cloudinary.uploader is undefined" while attempting to delete media files from Cloudinary

I'm currently developing a web application using express and node.js. In my project, I'm utilizing cloudinary for uploading media files. While uploading and accessing media works smoothly, I'm encountering an issue with deleting images from ...