Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Coffeescript access to current_user

I'm writing a Rails app.

In CoffeeScript, I would like to access the current_user along with it's relationship to an employee.

This is the code I'm trying:

    $.create "/events/",
      event:
        title: title,
        starts_at: "" + start,
        ends_at: "" + end,
        all_day: allDay,
        hours: hours,
        employee_id: current_user.employee_id
        # pickup the current employee

But, the console shows - Uncaught ReferenceError: current_user is not defined

Thanks in advance for the help!

like image 690
Reddirt Avatar asked Dec 11 '12 18:12

Reddirt


4 Answers

If you are writing coffescript inside rails assets pipeline (which I believe you do) you cannot access current_user at all. The problem here is that assets are precompiling once when you deploy to prod env (or on the first access in dev env). All subsequent requests just link to the precompiled js bundle. So js code is the same for all requests. On the other hand, current_user is specific for each request. So, in order to access current_user from the assets pipeline js code you have to request it from a specific server api method via some ajax call.

The other possible option is you are writing a coffeescript as controller action view template. In this case you should add .erb preprocessor. You'll get a file like app/views/controller_name/action_name.js.coffee.erb with

$.create "/event",
  event:
    employee_id: <%= current_user.employee_id %>

Hope this will help

like image 144
cryo28 Avatar answered Nov 19 '22 11:11

cryo28


I'm guessing that current_user in this context is a Ruby value that you're trying to access (a controller instance variable, or, based on the name, probably a Devise method that returns the current user object). This isn't going to be visible in JavaScript/CoffeeScript. You can set it in a view (e.g. ERB) and then access it in other JavaScript functions.

Check out this other Stack Overflow question for some good pointers on how to do this, including a link to a Railscast episode.

The short answer is that you'll probably want something like this in an ERB template:

<script>
    employee_id = <%= current_user.employee_id %>
</script>

Note that you can't just assign current_user to a JavaScript variable and then call methods on that from JS; it just doesn't work that way. You can't access Ruby/Rails values in JS; you have to store values that JS understands (scalars, JSON, whatever).

like image 44
Jim Stewart Avatar answered Nov 19 '22 12:11

Jim Stewart


I would suggest using the gem 'gon' , which is useful for passing data from ruby to coffee in rails application . Here is the Railscast about gon's usage , and this is the repository of 'Gon'.

like image 1
R Milushev Avatar answered Nov 19 '22 11:11

R Milushev


I suggest 3 options. But all of them are not secure:

  1. Some AJAX call from coffee to server (I haven't tried it).
  2. Rendering user_id as an attribute of a tag in your HTML view then acquiring it via jQuery in your coffee file. See https://www.sitepoint.com/create-a-chat-app-with-rails-5-actioncable-and-devise/

HTML:

<div id="user_id" data-user-id="<%= current_user.id %>">

CoffeeScript:

tag_user_id = $('#user_id')
current_user_id = tag_user_id.data('user-id')
  1. Setting user_id in plain cookie at login procedure and reading that cookie from inside coffee file as JS call document.cookie. If you use Devise, implement a Warden hook like here: https://rubytutorial.io/actioncable-devise-authentication/

app/config/initializers/warden_hooks.rb

Warden::Manager.after_set_user do |user,auth,opts|
  scope = opts[:scope]
  auth.cookies["#{scope}.id"] = user.id # not signed
end

CoffeeScipt:

current_user_id = document.cookie.replace(/(?:(?:^|.*;\s*)user.id\s*\=\s*([^;]*).*$)|^.*$/, "$1");
like image 1
prograils Avatar answered Nov 19 '22 11:11

prograils