Exploring how Ruby interacts with AngularJS variables in a view

Currently, I have implemented angular's ng-repeat in a Ruby .erb view. I now need to create a link_to a page that is dedicated to a specific IP address. Initially, I attempted:

<td><%= link_to '{{ roll.system }}' ,'server/'+'{{ roll.system }}' %></a></td>
,

where the {{roll.system}} represents the IP stored in an angular.js variable. The overall path would be localhost/server/127.0.0.1 but unfortunately, it doesn't work due to the dots in the IP address. So, my next approach is to turn the IP address into a hash and then decode it after routing.

My current code snippet is as follows:

<td><%= link_to '{{ roll.system }}' ,"server/"+Base64.urlsafe_encode64('{{ roll.system }} %></a></td>

The issue here is that Base64 encodes the string literally, resulting in a hash of the exact string '{{ roll.system }}'. What I actually need is for it to reference the value stored in the angular variable.

Though the link_to function seems to be working fine, I am struggling with this specific problem.

I kindly ask not to provide advice such as 'rewrite your app from scratch'.

Answer №1

There seems to be a mix-up between the rendering of a Rails view and the execution of Angular code.

Upon accessing your app, the Rails view is first rendered, converting the link_to into raw HTML like this:

<td><a href="{{ roll.system }}/server/{{ roll.system }}"></a></td>

After the view is rendered and displayed in the browser, the Angular code takes over, replacing the {{ roll.system }} placeholders with actual values:

<td><a href="127.0.0.1/server/127.0.0.1"></a></td>

The issue with the second approach is that the Base64.urlsafe_encode64 function runs during the Rails view rendering process, so the Angular script won't have updated the roll.system value yet. This results in static HTML regardless of the actual roll.system value:

 <td><a href="{{ roll.system }}/server/e3sgcm9sbC5zeXN0ZW0gfX0="></a></td>

An alternative solution would be to encode the string using base64 in the Angular script. You could explore libraries like angular-base64 for this purpose. Your code might look something like:

<td><a href="{{ roll.system }}/server/{{ roll.encoded_system }}"></a></td>

In this case, you would assign the value of roll.encoded_system in your Angular controller. As mentioned by Martin, you can also utilize the native window.btoa() method for encoding. It's worth noting that calling this method directly in the template may not work as expected and could require implementation within the controller.

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

Invoke a Google Apps Script through AJAX that necessitates authentication

I am looking to access a Google Apps Script via AJAX that requires user authorization to send an email from their Gmail account. The challenge lies in the fact that I need to send the email from within the Google Apps Script. The call originates from my w ...

Creating interactive HTML buttons using JavaScript to trigger AJAX requests

My current task involves populating an HTML table to showcase users. By making API calls to retrieve user data, I utilize Javascript to add rows to the table. Each row ends with a delete button, intended to trigger a $put request to a separate API endpoint ...

Transferring an array from PHP to jQuery

How can I pass an array from PHP to jQuery within the same file? I have an array named $array2 with data that I want to use to create a graph. The code below is currently using predefined chartData, but I would like to use a variable from my PHP script i ...

When using Nuxt JS and Jest, a warning message may appear stating "[Vue warn]: Invalid Component definition" when importing an SVG file

I am facing a unique issue only in my Jest unit test where an error occurs when I import an SVG into the component being tested: console.error node_modules/vue/dist/vue.common.dev.js:630 [Vue warn]: Invalid Component definition: found in -- ...

I am interested in altering the color of table rows using either JQuery or CSS

Looking to update row colors based on grouping. For example: Color the TR accordingly for every 5 rows: First 5 rows in green (0-4 Rows), Next five rows in red (5-9 Rows), Following five rows in yellow (10-14 Rows) and repeat the pattern... ...

Enhance your live table previews with the power of React JS

I'm trying to optimize the process of live updating a table in React. Currently, I am using setInterval which sends unnecessary requests to the server. Is there a more efficient way to achieve this without overwhelming the server with these unnecessar ...

Issue with server-side image rendering using base64 encoding not functioning as expected

My first post here comes with a huge thank you for all the anonymous help I've received in building my website. Please bear with me if the formatting of this question (and my code LOL) isn't perfect. I'm encountering some challenges with ren ...

Tips for identifying the most effective element locator in the DOM with Playwright

Currently, I am in the process of incorporating Playwright tests for a website that supports multiple locales. The majority of the page content is dynamically generated from CMS content (Contentful). I am hesitant about using hardcoded text locators like ...

Display HTML using JavaScript/jQuery

I am trying to figure out how to print a document by passing custom HTML code. Below is the code I have tried, but unfortunately it's not working: function Clickheretoprint() { var disp_setting="toolbar=yes,location=no,directories=yes,menubar=yes, ...

Real-time Broadcasts Straight to Your Web Browser

Feeling a bit frustrated with my current situation. I am eager to stream a live video broadcast to a web browser. At the moment, I am utilizing ffmpeg to stream a directshow live source as a webm stream to node.js, which then sends the stream to the http ...

Find the nearest minute when calculating the difference between two dates

To determine the difference between dates and round to the nearest minute, you can choose to either round date1 or date2 up or down. The result returned is already rounded up to the full minute. You have the flexibility to modify date1 and date2, but do no ...

The use of callback functions with Model.findOne() in Mongoose is now deprecated, and will result

Think about this: app.get("/posts/:postId", function(req, res) { const requestedPostId = req.params.postId; Post.findOne({_id: requestedPostId}, function(err, post) { res.render("post", { title: post.title, content ...

Utilize Python 3.7 to mark a checkbox on an HTML webpage

I am currently attempting to use Python 3.7 and Selenium to select a checkbox on an HTML page. My ultimate goal is to manipulate these checkboxes, but I am struggling to even select them correctly. The URL in question is: Prior to seeking assistance here, ...

Clicking on the tick box (submit) does not trigger any action

In my application, I have successfully used xeditable in various areas. However, there is one specific instance where clicking on the tick box does not trigger any actions: (I expect it to execute the onaftersave function in this case) <span editable- ...

Decode JSON and generate a user-friendly Array

My aim is to extract and organize the JSON data received from an external API into a custom array. However, I am encountering two challenges: I'm struggling to access the value labeled #2 under "Meta Data". If I want to extract the first array n ...

Error encountered: Jquery counter plugin Uncaught TypeError

I am attempting to incorporate the JQuery Counter Plugin into my project, but I keep encountering an error: dashboard:694 Uncaught TypeError: $(...).counterUp is not a function <!DOCTYPE html> <html lang="en"> <head> <script src ...

Replacing an Angular 2 application with a new webpage

I've been working on a project using Angular 2 and following this tutorial: Every time I run my api with npm run api on localhost:3000, the entire application gets replaced by another webpage. https://i.sstatic.net/5pIfX.png Here is my package.json ...

What is the definition of the term "WebapiError"?

I'm currently developing a Spotify Web App that focuses on retrieving the top albums of KD Rusha using the Client ID and Artist ID to exclusively fetch his releases on Spotify. To accomplish this, I am utilizing an npm package called spotify-web-api-n ...

Efficiently perform complex nested grouping using the powerful array

Hi, I'm currently facing difficulties while attempting to utilize the array.reduce() function in order to achieve correct grouping for a specific scenario: Here is my original array: { { descriptionFunction: "Change", processDate: "201 ...

Node interprets string as an object

I encountered the following code: <a class="btn-primary btn" href="/employer/booth/<%= user._id.toString() %>" <% console.log(typeof user._id, user._id) %> target="_blank"> Preview your booth </a> Upon running this ...