# Javascript to access a server side element in rails

**URL:** <https://www.ruby-forum.com/t/javascript-to-access-a-server-side-element-in-rails/181230>\
**Category:** Rails\
**Created:** [January 7, 2010, 7:26pm UTC](https://www.ruby-forum.com/t/javascript-to-access-a-server-side-element-in-rails/181230 "2010-01-07T19:26:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![nisha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/76d3ee/32.png) [@nisha](https://www.ruby-forum.com/u/nisha)\
**Post date:** [January 7, 2010, 7:26pm UTC](https://www.ruby-forum.com/t/javascript-to-access-a-server-side-element-in-rails/181230/1 "2010-01-07T19:26:05Z")

</div>

Hi all,

TestController

def show  
@object = Object.find(params[:id])  
end

I have show.html.erb which has the foll js in the head

This basically dynamically loads the first.html file after the set  
interval…

The first.html.erb is as follows

script type=‘text/javascript’\>  
$(document).ready(function($) {  
//how to access @object.attribute?  
})

Can anyone pls guide me how to access @object.attribute in  
first.html.erb’s js to load the html in the container.

Pls help

---

<div class="post-metadata">

**Author:** ![nisha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/76d3ee/32.png) [@nisha](https://www.ruby-forum.com/u/nisha)\
**Post date:** [January 8, 2010, 5:47pm UTC](https://www.ruby-forum.com/t/javascript-to-access-a-server-side-element-in-rails/181230/2 "2010-01-08T17:47:38Z")

</div>

nisha,

Did you try with \<% %\> or \<%= %\> tags in the html.erb. Something like

\<%= @object.attribute %\>

I am not sure , what problem you are facing?

Regards,  
Naren

---

<div class="post-metadata">

**Author:** ![nisha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/76d3ee/32.png) [@nisha](https://www.ruby-forum.com/u/nisha)\
**Post date:** [January 9, 2010, 10:43am UTC](https://www.ruby-forum.com/t/javascript-to-access-a-server-side-element-in-rails/181230/3 "2010-01-09T10:43:00Z")

</div>

On Thu, Jan 7, 2010 at 9:45 AM, nisha [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> interval... Can anyone pls guide me how to access @object.attribute in first.html.erb's js to load the html in the container. Pls help

If you’re trying to access server-side Ruby objects within the  
client-side  
Javascript, then this is possible by  
making an Ajax call to the server and returning the result as JSON or  
some  
other formatted data.

If you’re trying to access server-side Ruby objects within the HTML,  
then  
you can simply do the following:

\<%= @object.attribute %\>

Good luck,

-Conrad
