0

I have a code like this in my app/assets/javascripts/sample.js.erb file.

  var user_signed_in = <%= user_signed_in? %>;
  if(user_signed_in){
      // run code here
  }

By the looks of it, with the .js.erb file extension. This should work. I am able to use the user_signed_in? in the view that uses this JS file. So why?

Ricardo Green
  • 1,058
  • 1
  • 11
  • 27
  • try this ` var user_signed_in = "<%= user_signed_in? %>";` – Vishal Sep 06 '18 at 03:36
  • @Vishal does not work. – Ricardo Green Sep 06 '18 at 03:39
  • It should work in this way, put `console.log("<%= user_signed_in %>") ` let me know what you get – Vishal Sep 06 '18 at 03:41
  • I still get the error: `undefined local variable or method `user_signed_in'`. – Ricardo Green Sep 06 '18 at 03:43
  • 3
    shouldn't it be ? `<%= user_signed_in? %>`. Also, can you add this in `application` layout. – Kedarnag Mukanahallipatna Sep 06 '18 at 03:45
  • try to use with `"<% user_signed_in? %>"` instead of `"<%= user_signed_in? %>"` – Vishal Sep 06 '18 at 03:45
  • @KedarnagMukanahallipatna Yes right, it should be like this. and he is used that – Vishal Sep 06 '18 at 03:47
  • @KedarnagMukanahallipatna already added it in the application.js file. Still not working. Your suggestion for `<%= user_signed_in? %>` is whats in my question and is not working. – Ricardo Green Sep 06 '18 at 03:50
  • Tried that `"<% user_signed_in? %>" `. Still not working @Vishal. – Ricardo Green Sep 06 '18 at 03:52
  • Let me know, what you get when you do `console.log("<% used_signed_in? %>")` and `console.log('<% "#{user_signed_in?}" %>')` – Vishal Sep 06 '18 at 03:53
  • I get errors since `user_signed_in?` is an undefined method, according to the error. @Vishal. – Ricardo Green Sep 06 '18 at 03:59
  • Have you tried using [gon](https://github.com/gazay/gon) ? – Kedarnag Mukanahallipatna Sep 06 '18 at 04:16
  • Were not considering a new gem for this, because we are only using this for one scenario. Anyways, thanks @KedarnagMukanahallipatna. – Ricardo Green Sep 06 '18 at 04:26
  • 2
    Why do you have a _*.js.erb_ file under _app/assets/javascripts/_? Shouldn't you be using it in some view under _app/views/_? – Jagdeep Singh Sep 06 '18 at 04:37
  • @JagdeepSingh we have a page where we want to check if a user us signed in when he/she visits it on the JS level. We also could possibly use it on other pages in the future. So we want it to be inside an independent js.erb file. – Ricardo Green Sep 06 '18 at 04:40
  • @JagdeepSingh hats of to your eyes. i didn't notice that – Vishal Sep 06 '18 at 05:06
  • Try: `var user_signed_in = <%=raw user_signed_in? %>;` – Emu Sep 06 '18 at 05:14
  • @Emu not working. – Ricardo Green Sep 06 '18 at 05:44
  • 1
    @RicardoGreen Basically, why any solutions not working because your are not following the concept of ror, you should not use js.erb file in js folder, "user_signed_in?" is just helper and you can't access outside of view folder. you are trying to access in js folder. that's why it is giving you error. please go and follow the correct structure – Vishal Sep 06 '18 at 06:37
  • I think you are not following the right structure. Check this [link](http://railsapps.github.io/rails-javascript-include-external.html#locations) which might help you. Also, a similar question was made [reference](https://stackoverflow.com/questions/46534739/how-to-use-javascript-in-ruby-on-rails) – John Baker Sep 06 '18 at 12:27

1 Answers1

0

Try to make something like this

<% if current_user  %>
  <%= link_to "Add New Event",new_event_path %>
<% end %>
nourza
  • 2,215
  • 2
  • 16
  • 42