2

I want to use the current user's through my coffee script. But if I try to use current_user in my CoffeeScript, I get the undefined variable current_user in console of browser.

Is there any way to access it in coffee script? What I exactly wish to do is as follows:

current_user.updated_at < Date.now().getTime()

I need it to implement this in my Rails app.

2
  • You could try looking at this gem github.com/gazay/gon. I haven't used it myself but others have recommended it here occasionally. Commented Jan 23, 2017 at 10:23
  • 2
    What's that codeline? :O I really don't get it what you want to do.. On the left side there's ruby code and on the right there's Javascript?! ლ(ಠ益ಠლ) Commented Jan 23, 2017 at 10:37

3 Answers 3

2

For raw javascript + erb

<script type="text/javascript">
  var my_foo = <%= some_ruby_expression %>
</script>

e.g.

<script type="text/javascript">
  var user_name = "<%= current_user.name %>";
</script>

except var keyword, this is a valid coffeescript code, I believe.

Btw, is it really what you are asking for? - I don't know -))

UPDATE

current_user is a ruby/active_record object. You might assign it to a javascript variable, but can't use as you did in ruby.

But below snippet might give you some idea. I've created a rails project, and scaffolded a page model.

<p id="notice"><%= notice %></p>

<p>
  <strong>Title:</strong>
  <%= @page.title %>
</p>

<p>
  <strong>Email:</strong>
  <%= @page.email %>
</p>

<p>
  <strong>Comments:</strong>
  <%= @page.comments %>
</p>

<script type="text/javascript">
    var mypage = '<%= raw @page.to_json %>'; // attention to single quotes
    console.log(mypage);
</script>

<%= link_to 'Edit', edit_page_path(@page) %> |
<%= link_to 'Back', pages_path %>

Then you can parse and use it as an ordinary javascript object. Below picture show, how I did it.

enter image description here

UPDATE #2

do it in coffeescript file.

window.onload = ->
  myvar = '<%= raw @page.to_json %>'
  myvarAsObj = JSON.parse(myvar)
  do_something_with myvarAsObj
  return
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for the answer, but I want to use current_user which is a helper's variable provided by devise gem.
@siegyy22 Not working. But anyhow even to make this work, I have to write it in that page itself. But i wish to write it on .coffee.erb file. Do you have any idea regarding it? Thanks :)
@siegy22 Yes i saw your answer. But i haven't run the app in production mode.
@marmeladze Thanks for your effort, but i need to access those in coffee script file that is under app/assets/javascript folder. (i.e. in asset pipeline).
|
0

Coffee script is turned into javascript and runs on the browser. What you want to do is render current_user.updated_at into a node in HTML, then use coffee script to extract that value.

1 Comment

Thanks for the reply. but is it possible with any other way?
0

Javascript files are (more or less) static files which will be executed by the browser. I don't know if you ever deployed an rails app in production. You'll run the command bin/rake assets:precompile which will generate one (or more) massive javascript file(s). So how would you find out the current user at this time? there's no HTTP request at all, so how could there be a user?

So to do this, you could either write the current user to a HTML node and then read it again with javascript (coffeescript) (which in my opinion is the best way) or you could print a script tag into your view containing what you want from your user.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.