What is the best way to transfer my current_user.id to an AngularJS Controller?

Currently, I am retrieving my current_user.id from Ruby on Rails and attempting to transfer it to my angular controller using an HTML hidden input.

<input type="hidden" ng-model="user_id" value="<%= current_user.id %>"></input>

While debugging, I noticed that my scope is undefined for "$scope.user_id", which is crucial for sending the value to my server.

Could there be a mistake in my approach? Is there a more efficient method available?

Answer №1

attempt

<input type="hidden" 
       ng-bind="user_id" 
       ng-init="user_id = <% equals current_user.id %>"></input>

Answer №2

In my opinion, the best choice is to implement Devise Token Auth along with ng-token-auth. This combination seamlessly integrates all of Devise's features into Angular, providing a robust solution. Using ng-token-auth opens up endless possibilities for customization and beyond.

Answer №3

Instead of using ng-token-auth, you have the option to create an Angular service with a Ruby helper.

    # layout/application.html.slim
    = js_current_user

    # application_helper.rb
    def js_current_user
      <<-EOS.html_safe
        <script type="text/javascript">
          angular.module('myApp').constant('CURRENT_USER', #{current_user.to_json})
        </script>
      EOS
    end

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

Embed the json data within the modal box

I received a JSON response from a php function, and it looks like this: [{"id":"15","activity_type":"call","activity_title":"Call to check "}] Below is the script that triggers the request (actvitiy.js) (Edited) $(document).on("click", ".view_contact_ac ...

Issue arises when component is mistakenly displayed in the header upon deployment on Vercel platform

When deploying my NextJS app to Vercel, I encounter an issue where state-based modals that should be hidden are displaying. Additionally, the modals are rendering in the header instead of center-screen as expected. Surprisingly, these problems do not occur ...

What is the best way to implement a nested side menu in Ionic Framework?

Greetings! I'm currently attempting to incorporate a nested menu within a side menu. For instance, take a look at this example: http://codepen.io/ionic/pen/QwamEW When I select a menu item, a new page (new form) opens. However, I would like for th ...

Maintaining accurate type-hinting with Typescript's external modules

Before I ask my question, I want to mention that I am utilizing Intellij IDEA. In reference to this inquiry: How do you prevent naming conflicts when external typescript modules do not have a module name? Imagine I have two Rectangle classes in different ...

What is the best way to wait for a button's listeners to resolve in JavaScript?

Currently, I am conducting frontend tests utilizing Jest with the jsdom environment to simulate a DOM tree and manually trigger actions such as button.click(). My goal is to be able to await button.click(), which in my expectations should wait for all of ...

the nodejs app cannot be launched as the port is already being utilized

I've encountered an issue while trying to run my nodejs app. It's displaying an error indicating that the port is already in use. I've made several attempts to resolve this problem by restarting the application. Error: listen EADDRINUSE: a ...

Function modification led to Sinon test failure

I am currently facing an issue with my test: describe('createNote', () => { beforeEach(() => { res = { json: sinon.spy(), sendStatus: sinon.spy(), }; }); afterEach(() => { noteService.createUserNote.restor ...

Are there any easier methods for locating the coordinates of a state?

I am currently working on my first JS project, which involves creating an interactive map with markers for each state in the US. Using Leaflet.js, I have manually inputted the name and coordinates of every state in alphabetical order. Below is a snippet o ...

Setting up Highcharts version 7 heatmaps with npm in an Angular 6 environment

I am currently having an issue with initializing the heatmap lib in Highcharts 7.0.1 within my Angular 6 app via npm. Despite successfully running basic charts like line, spline, bar, etc., when following the documentation for the heatmap initialization, I ...

What is the best way to fix multiple dropdown menus opening simultaneously in Vue.js?

I am working on an application that generates todo lists for tasks. These lists can be edited and deleted. I am trying to implement a dropdown menu in each list to provide options for updating or deleting the list individually. However, when I click on the ...

Utilize AJAX/JSON to populate your Highcharts series with dynamic data

My chart/graph is not displaying the data from my AJAX/JSON response properly within the <div class="statsrep"></div> container. The categories/dates are showing incorrectly, and I'm unsure of what the cause might be. Could you help me de ...

Exploring AngularJS and Jasmine: Testing a controller function that interacts with a service via $http

I encountered an issue while testing a controller that relies on a service. The problem arises because the service is currently set to null in order to focus solely on testing the controller. The current test setup is failing due to the BoardService being ...

Is it possible to utilize an npm package in TypeScript without a d.ts definition file?

Is it possible to use an npm package in TypeScript and Node.js without a .d.ts definition file? If so, how can I make it work? Currently, my code looks like this and I'm getting an error that says "cannot find module 'node-rest-client'" bec ...

Node.js is having trouble locating a module that has been installed

Upon transferring my node.js application to a different PC (where it functioned flawlessly on the development machine) and manually installing all the necessary dependencies, I encountered an error when attempting to run it: C:\Users\myself>n ...

Anticipated a response in the form of an object, however, a string was delivered instead

After recently updating to v14 of discordjs, I've been encountering numerous errors, including the one below. It seems a lot has changed since my last coding session, and I'm a bit unsure about what modifications are needed to ensure this ping sl ...

Automapper for AngularJS to format results retrieved from a RESTful service

When using the $resource service for data access from a restful service, I encounter a small challenge. The JSON data retrieved is in the following format: { "name_surname": "john_smith", "years_of_employment": "10" } However, I would like to map ...

AngularJS $http Wrapper for Service Providers

Is it possible to create a custom provider in AngularJS that can replace the $http operation? This would allow us to use this provider in other modules and make use of the $http operations. The reason for considering a provider is because we can configure ...

Leveraging ng-repeat within ng-view

I am facing an issue with using ng-repeat inside ng-view as the data is not being displayed. I have come across suggestions on forums to use a factory, but I am hesitant to utilize a service because my $scope data relies on $routeParams for querying. var ...

Using vue-resource to intercept an ajax error and catching the "Uncaught (in promise)" exception

I am utilizing vue-resource to retrieve data from the server. In order to access the correct data, a user must possess a JWT token. If the token is invalid or has expired, a status code of 401 will be returned. Similarly, if a user attempts to reach a forb ...

What is the best way to transfer a variable from a form to a PHP page using an AJAX script?

Here is the form I am working with: <form onsubmit="serverconnect('Div1', 'query.php'); return false;" Minimum Discount: <br /> <input type="radio" value="20%" name="discount" /> 20% <br /> <input type="radi ...