# Session nil in view

**URL:** <https://www.ruby-forum.com/t/session-nil-in-view/128423>\
**Category:** Rails\
**Created:** [February 7, 2008, 3:58pm UTC](https://www.ruby-forum.com/t/session-nil-in-view/128423 "2008-02-07T15:58:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![MohsinHijazee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e68b1a/32.png) [@MohsinHijazee](https://www.ruby-forum.com/u/MohsinHijazee)\
**Post date:** [February 7, 2008, 3:58pm UTC](https://www.ruby-forum.com/t/session-nil-in-view/128423/1 "2008-02-07T15:58:12Z")

</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.[]  
On line #13 of admin/details/new.rhtml

```
10: <% if params["for_entity"] %>
11: <%= submit_tag(t("quick_submit"), :name => "quick_commit")%> |
12: <%end%>
13: <%=link_to t("cancel"),session["return-to"] %>
14: <% end -%>
15:
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:** ![MohsinHijazee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e68b1a/32.png) [@MohsinHijazee](https://www.ruby-forum.com/u/MohsinHijazee)\
**Post date:** [February 7, 2008, 6:03pm UTC](https://www.ruby-forum.com/t/session-nil-in-view/128423/2 "2008-02-07T18:03:32Z")

</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://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e68b1a/32.png) [@MohsinHijazee](https://www.ruby-forum.com/u/MohsinHijazee)\
**Post date:** [February 8, 2008, 6:31am UTC](https://www.ruby-forum.com/t/session-nil-in-view/128423/3 "2008-02-08T06:31:21Z")

</div>

On Feb 7, 10:02 pm, bigbanger [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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.

---

<div class="post-metadata">

**Author:** ![MohsinHijazee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e68b1a/32.png) [@MohsinHijazee](https://www.ruby-forum.com/u/MohsinHijazee)\
**Post date:** [February 8, 2008, 10:18am UTC](https://www.ruby-forum.com/t/session-nil-in-view/128423/4 "2008-02-08T10:18:07Z")

</div>

On 8 Feb 2008, at 05:30, MohsinHijazee wrote:

> 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://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e68b1a/32.png) [@MohsinHijazee](https://www.ruby-forum.com/u/MohsinHijazee)\
**Post date:** [February 8, 2008, 11:30am UTC](https://www.ruby-forum.com/t/session-nil-in-view/128423/5 "2008-02-08T11:30:46Z")

</div>

On Feb 8, 2:17 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > But that piece of code works fine when I browse through my browser in

Thank you very much!
