0

I had this working then came back to my app after a few months now it's broken. I am sure it's a subtle change in a gem somewhere or I am missing something obvious.

In my application controller I have this:

  def after_sign_in_path_for(resource)

    user_session["company_id"] = current_user.default_company_id
    cookies[:login] = { :value => current_user.email, :expires => Time.now + 2.weeks}
    dashboard_path
  end

If I call user_session["company_id"] later it's nil (current_user.default_company_id works).

If I set user_session["company_id"] = current_user.default_company_id in the code just before where I call user_session["company_id"] to fetch the value it works.

2
  • why do you use user_session and not session ? Commented May 13, 2015 at 6:47
  • I saw that used in another example and just carried that over. I even tried using session and that didn't work either. Commented May 13, 2015 at 6:49

1 Answer 1

1

You can set company_id in a method calling before_filter in application controller as following

before_filter :set_company_id

def set_company_id
    session["company_id"] = session["company_id"] || current_user.default_company_id if current_user
end

Erase setting company_id in after_sign_in_path_for method

def after_sign_in_path_for(resource)
    cookies[:login] = { :value => current_user.email, :expires => Time.now + 2.weeks}
    dashboard_path
end
Sign up to request clarification or add additional context in comments.

3 Comments

Yes - that is correct but to clarify in my situation I want to set the session["company_id"] after login. The user then can "switch" companies and the session["company_id"] gets changed. In this case the current company id would always revert to the default.
I have edited the answer, before set company_id, you should check if any company_id is there it will not reset to default company id. wish you will get an idea from this.
That should work - the question that still nags at me is why does what I had originally not work? I had it working like that before so I would still like to know what changed.

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.