【发布时间】:2011-07-20 01:22:20
【问题描述】:
在我看来,我正在尝试将一些数据传递给 javascript。我只想要数组中对象的某些属性。
json gem 似乎不支持:only 选项。我尝试使用 ActiveSupport::JSON
<script>
test1=<%=raw ActiveSupport::JSON.encode(@sectionDatas.values, :only => [ :left, :width ])%>;
</script>
但这会忽略:only 并打印整个对象。
然后我想我会很聪明,从控制器中获取render 方法:
test2=<%=raw render :json => @sections.as_json(:only => [:left, :width])%>
但我得到 Nil:Nilclass 错误。
我也尝试将它放入我的模型中并运行 to_json:
include ActiveModel::Serialization::JSON
def attributes
@attributes ||= {'left' => 0, 'width'=>0}
end
同样,这会忽略属性方法并序列化整个对象。
当然,这一定很简单。我错过了什么?
【问题讨论】:
标签: ruby-on-rails ruby ruby-on-rails-3 json