Javascript to access a server side element in rails

Hi all,

TestController

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

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

<script type='text/javascript'>
    $(document).ready(function($)
    {
        $("#divcontainer").load("first.html.erb");
      var refreshId1 = setInterval(function() {$("#divcontainer").load
('first.html.erb');}, 120000);
               })
</script>

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?
      })
</script>
<div class='test'></div>

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

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

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

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