設定したい仮想属性を持つ投稿モデルがあり、post#index アクションへの JSON 呼び出しへの応答に含めます。仮想属性を応答に含めることができないようです。
class Post < ActiveRecord::Base
attr_accessible :height
attr_accessor :m_height
end
class PostsController < ApplicationController
respond_to :html, :json, :js
def index
story = Story.find(params[:story_id])
@posts = story.posts.where("posts.id >= ?", 100)
@posts.each do |post|
post.m_width = post.height * 200
end
results = { :total_views => story.total_views,
:new_posts => @posts }
respond_with(results)
end
end
に似たものが必要だと思います@post.to_json(:methods => %w(m_width))
が、respond_with で :methods を使用する方法がわかりません