Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 846427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T06:36:45+00:00 2026-05-15T06:36:45+00:00

I have a website where I need a javascript version of the current user

  • 0

I have a website where I need a javascript version of the “current user” object along with the ruby version. I have been assigning these variables doing something like this…

Application Controller:
def get_user
    begin
      @current_user = User.find(session[:user_id]) if session[:user_id]
      @current_user_json = @current_user.to_json
    rescue
      session.delete(:user_id)
      @current_user = nil
      @current_user_json = {}
    end
  end

Web Page:
var current_user = null;
current_user_json = '<%= @current_user_json %>';
if(current_user_json != ''){
        current_user = current_user_json.user;
}

Even when there is a current user, I get the current user is undefined. Probably because I am putting the current_user_json assignment around single quotes. However, if I don’t put it around single quotes, I’ll always get a javascript error when no user is logged in because the syntax is invalid –

current_user_json = ;

I think I am just looking at this completely wrong and there must be a better way. Given that this is probably a common thing to do, I wanted to get other people’s opinion on how to create an object in javascript that is a duplicate of the ruby object.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T06:36:46+00:00Added an answer on May 15, 2026 at 6:36 am

    JSON is valid Javascript. Consider removing the quotes and just outputting it directly:

    current_user_json = <%= @current_user.nil? ? '' : @current_user_json %>;
    

    Better yet, have your controller do all the work instead of putting logic in the view:

    @current_user_json = @current_user.nil? ? '{user: null}' : @current_user.to_json 
    # ...
    current_user_json = <%= @current_user_json %>;
    

    (EDIT: Incorporated Pointy’s suggestion below.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 416k
  • Answers 416k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Really easy updating can come through the detailsview control: https://web.archive.org/web/20211020133929/https://www.4guysfromrolla.com/articles/050207-1.aspx May 15, 2026 at 9:11 am
  • Editorial Team
    Editorial Team added an answer A non-standard attribute can be anything. Some common examples are:… May 15, 2026 at 9:11 am
  • Editorial Team
    Editorial Team added an answer In the file that owns the tab bar controller insert… May 15, 2026 at 9:11 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.