# session nil in view

**URL:** <https://discuss.rubyonrails.org/t/session-nil-in-view/24506>\
**Category:** rubyonrails-talk\
**Created:** [February 7, 2008, 2:57pm UTC](https://discuss.rubyonrails.org/t/session-nil-in-view/24506 "2008-02-07T14:57:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![MohsinHijazee](https://avatars.discourse-cdn.com/v4/letter/m/e68b1a/32.png) [@MohsinHijazee](https://discuss.rubyonrails.org/u/MohsinHijazee)\
**Post date:** [February 7, 2008, 2:57pm UTC](https://discuss.rubyonrails.org/t/session-nil-in-view/24506/1 "2008-02-07T14:57:37Z")

</div>

I have to provide a link based on a value stored in session object but getting the session object nil in the view.

Following is the error message:

ActionView::TemplateError: You have a nil object when you didn't expect it! You might have expected an instance of ActiveRecord::Base. The error occurred while evaluating nil. &nbsp;&nbsp;&nbsp;&nbsp;On line #13 of admin/details/new.rhtml

&nbsp;&nbsp;&nbsp;&nbsp;10: \<% if params["for\_entity"] %\> &nbsp;&nbsp;&nbsp;&nbsp;11: \<%= submit\_tag(t("quick\_submit"), :name =\> "quick\_commit")%\> | &nbsp;&nbsp;&nbsp;&nbsp;12: \<%end%\> &nbsp;&nbsp;&nbsp;&nbsp;13: \<%=link\_to t("cancel"),session["return-to"] %\> &nbsp;&nbsp;&nbsp;&nbsp;14: \<% end -%\> &nbsp;&nbsp;&nbsp;&nbsp;15: &nbsp;&nbsp;&nbsp;&nbsp;16: \<script type="text/javascript"\>

Can anyone guide me how the session object is fine in the controller and the very next instant its nil in the view?

Regards, Mohsin

---

<div class="post-metadata">

**Author:** ![bigbanger](https://avatars.discourse-cdn.com/v4/letter/b/e56c9b/32.png) [@bigbanger](https://discuss.rubyonrails.org/u/bigbanger)\
**Post date:** [February 7, 2008, 5:02pm UTC](https://discuss.rubyonrails.org/t/session-nil-in-view/24506/2 "2008-02-07T17:02:14Z")

</div>

The session object is not accessible in a view, it is only available in the controller layer. But you can save the session value in an instance variable in your controller and reference that instance variable in your view instead.

---

<div class="post-metadata">

**Author:** ![MohsinHijazee](https://avatars.discourse-cdn.com/v4/letter/m/e68b1a/32.png) [@MohsinHijazee](https://discuss.rubyonrails.org/u/MohsinHijazee)\
**Post date:** [February 8, 2008, 5:30am UTC](https://discuss.rubyonrails.org/t/session-nil-in-view/24506/3 "2008-02-08T05:30:34Z")

</div>

But that piece of code works fine when I browse through my browser in the development mode. This only happens when I run the tests.

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [February 8, 2008, 9:17am UTC](https://discuss.rubyonrails.org/t/session-nil-in-view/24506/4 "2008-02-08T09:17:28Z")

</div>

> > The session object is not accessible in a view, it is only available in the controller layer. But you can save the session value in an instance variable in your controller and reference that instance variable in your view instead.
> 
> But that piece of code works fine when I browse through my browser in the development mode. This only happens when I run the tests.

Would have been useful if that's what you had said? I recall a recent discusscion on rails core where it emerged that normally the session is a HashWithIndifferentAccess (so session["return-to"] and session[:return-to] are the same thing, but in tests it was just a regular hash( and so only session[:return-to] worked). Use session[:return-to] instead of the string form and you should be ok.

Fred

---

<div class="post-metadata">

**Author:** ![MohsinHijazee](https://avatars.discourse-cdn.com/v4/letter/m/e68b1a/32.png) [@MohsinHijazee](https://discuss.rubyonrails.org/u/MohsinHijazee)\
**Post date:** [February 8, 2008, 10:30am UTC](https://discuss.rubyonrails.org/t/session-nil-in-view/24506/5 "2008-02-08T10:30:07Z")

</div>

> \>\> The session object is not accessible in a view, it is only available \>\> in the controller layer. But you can save the session value in an \>\> instance variable in your controller and reference that instance \>\> variable in your view instead.
> 
> \> But that piece of code works fine when I browse through my browser in \> the development mode. This only happens when I run the tests.
> 
> Would have been useful if that's what you had said? I recall a recent discusscion on rails core where it emerged that normally the session is a HashWithIndifferentAccess (so session["return-to"] and session[:return-to] are the same thing, but in tests it was just a regular hash( and so only session[:return-to] worked). Use session[:return-to] instead of the string form and you should be ok.
> 
> Fred

Thank you very much!
