Steps for adding user identification to a CoffeeScript file

I'm facing a dilemma. I have a User model, an Event model, and also using fullcalendar.

$(document).ready ->
  $("#calendar").fullCalendar(
    events: '/events.json'
  )

In my coffeescript file, the code isn't working because it requires

events: '<%<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e4d9a491978196ca8d80">[email protected]</a>%>/events.json'
. However, since ruby-code cannot be used in a coffeescript file, I need to access user_id.

Any suggestions on how I can achieve this?

Answer №1

Generating the HTML allows for flexibility in assigning user IDs. One method involves adding JavaScript code at the bottom of your file to set the user's ID as a globally accessible configuration variable.

For instance, in an ERB file:

<%= javascript_tag do %>
  window.Configuration = {};
  window.Configuration.UserId = <%= @user.id %>
<% end %>

Then, in your CoffeeScript file:

events: "#{window.Configuration.UserId}/events.json"

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

Creating an Angular directive that handles asynchronous attribute interpolation

I am facing an issue with my custom directive. In the link function attributes, I am trying to access the value of attributes.user. Here is how the directive is used in my view page: <div my-directive user="{{user.name}}"></div> The user obje ...

Encountering an error while attempting to launch an AngularJS application on Node.js? Let's

Currently, I am in the process of learning Angular. Whenever I attempt to run my code, an error message pops up: > <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1f7a737a7c6b6d70715f2b312f3115">[email protected]< ...

I am looking to create a side menu that will allow for dynamic class changes upon clicking

I'm looking to create a dynamic side menu that changes class on click event. My goal is to have jQuery add a class to a clicked "li" element that doesn't already have the class "active", while removing the class from other "li" elements. I want ...

Combine an entire Yeoman project built with AngularJS into a single file for seamless integration

Currently in the process of creating a form using Yeoman and AngularJS version 1.2.27. The goal is to have this form easily integrated into any client's web page. By generating a distribution folder with grunt, containing various files like JS, CSS, ...

Creating a Selectable Child Form in ReactJS that Sends Data to Parent Form

Sorry for the lack of details, I'm struggling to explain this situation clearly. I'm currently learning ReactJS and JS. In a project I am working on, I have the following requirements: There is a form where users can input text and numbers. The ...

send the user to a different page while transferring post data using Javascript

Currently, I have an HTML form that utilizes the post method to send data to a PHP page for processing into a MySQL database. However, before the user submits the form, there is a button that allows them to view a PDF of the entered data on a new page. Whi ...

JavaScript code that loads a specific DIV element only after the entire webpage has finished loading

How can I ensure that the DIV "image" is loaded only after the entire page has finished loading? What JavaScript code should I use? <div class="row"> <div class="image"> CONTENT </div> </div> I plan to execute the functio ...

Refresh the Google Maps location using GPS coordinates

Currently, I am working with an Arduino that has a GPS chip and processing NMEA strings with Python. I have an HTML file set to auto-refresh every x seconds to update the marker's position. However, I would like to update the position information with ...

How do I return a <div> element to its initial state after JavaScript has made changes to it?

So, I have this situation where a DIV contains a form. After users submit the form successfully, I want to replace the form with a simple message saying "everything is good now". This is how I currently do it: $("#some_div").html("Yeah all good mate!"); ...

There appears to be a issue with BINDING functionality within Angular

When using two select fields, selecting an item in the first should automatically fill the second with the corresponding subarray. While this functionality works, adding a new field results in all other selections changing as well. How can I prevent this f ...

Endless loop in React Native with an array of objects using the useEffect hook

For the current project I am working on, I am facing the challenge of retrieving selected items from a Flatlist and passing them to the parent component. To tackle this issue, I have initialized a local state as follows: const [myState, setMyState] = useS ...

The issue with EJS Header and Footer not showing up on the

I am currently building a basic todo list application using EJS. However, I am encountering an issue when running it. I would like to run app.js with the header and footer in EJS, but it seems that it is not recognizing the header despite using what I beli ...

Minimize the number of clicks needed to navigate using the HTML/JavaScript navigation

On my website, I have a navigation bar that changes the contents of a div when a user clicks on an item. However, if the user clicks multiple times in quick succession, the content changes too many times. I want to ensure that the content only changes once ...

What is the best way to rearrange (exchange) elements within an Immutable Map?

Is there a way to rearrange items within an unchangeable list that is part of a Map? Here's an example: const Map = Immutable.fromJS({ name:'lolo', ids:[3,4,5] }); I have attempted to use the splice method for swapping, as well as ...

Assign different colors to headings based on their associated button ids using JQuery

I have a limited number of buttons and headings to work with. Here is the HTML code structure: <button id = "1">1</button> <button id = "2">2</button> <heading id="1">heading1 <button>cli ...

Controlling an AJAX request to fetch individuals

I am currently using an ajax request to display people data on the page, which is retrieved from a backend database. The search form on the page includes options for location, sector, service, and job title. Below is the javascript code being used: //var ...

Showcasing text behind an element with reduced opacity when the element directly above it is selected using rails, CSS, and jQuery

I have a password field on my page where the password is hidden, but I want users to be able to copy and paste the clear text version of the password on another website. In order to achieve this, I followed the instructions in an article titled Mask text, ...

Removing HTML elements and accounting for new lines in a text box

I am currently utilizing CodeIgniter for my personal website, which includes multiple textareas. I am looking for a solution to prevent HTML tags from being stored in the database. Is there a tool available that can strip out any unwanted tags? Additional ...

Sending a Boolean value from a child component to its parent state in React JS

Within my application, I have implemented a login feature in the navbar component. However, I am encountering an issue with updating a variable in the main component upon successful login. Although I have successfully managed to set up the login functional ...

The issue encountered with the Material UI Autocomplete component is that it is not displaying the values

I'm currently attempting to extract a value from the state to be used in materialUI's autocomplete component. However, I've encountered an issue: The autocomplete feature works perfectly when selecting a value and saves it to the state wit ...