I'm trying to pass some data to javascript in my view. I only want certain attributes of the objects in the array.
The json gem doesn't appear to support the :only
option. I tried to use ActiveSupport::JSON
<script>
test1=<%=raw ActiveSupport::JSON.encode(@sectionDatas.values, :only => [ :left, :width ])%>;
</script>
but that ignores the :only
and prints the whole object.
Then I thought I would be clever 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 to put this in my model and run to_json:
include ActiveModel::Serialization::JSON
def attributes
@attributes ||= {'left' => 0, 'width'=>0}
end
Again, this ignores the attributes method and serializes the whole object.
Surely this must be simple. What am I missing?