I am trying to pass some data to javascript in my opinion. I need only certain attributes of the objects in the array.
The json driver does not support the :only option. I tried using ActiveSupport :: JSON
<script> test1=<%=raw ActiveSupport::JSON.encode(@sectionDatas.values, :only => [ :left, :width ])%>; </script>
but ignores :only and prints the entire object.
Then I thought that I would be smart and take the render method from the controller:
test2=<%=raw render :json => @sections.as_json(:only => [:left, :width])%>
but i get nil: nilclass errors.
I also tried putting this in my model and running to_json:
include ActiveModel::Serialization::JSON def attributes @attributes ||= {'left' => 0, 'width'=>0} end
Again, this ignores the attribute method and serializes the entire object.
Of course, this should be simple. What am I missing?
source share