我有一个语言模型,我想将所有语言作为 json,但 json 输出如下所示
[{"language":{"created_at":null,"id":1,"language":"English","updated_at":null}},{"language":{"created_at":null,"id" :2,"language":"Swedish","updated_at":null}},{"language":{"created_at":null,"id":3,"language":"German","updated_at":null }},{"language":{"created_at":null,"id":4,"language":"French","updated_at":null}},{"language":{"created_at":null," id":5,"language":"spanish","updated_at":null}},{"language":{"created_at":null,"id":6,"language":"dutch","updated_at" :null}},{"语言":{"created_at":"2012-12-03T05:01:18Z","id":7,"language":"泰米尔语","updated_at":"2012-12-03T05:01:18Z"}}]
但我想把它变成
{"language":[{"created_at":null,"id":1,"language":"English","updated_at":null},{"created_at":null,"id":2,"language" :"瑞典语","updated_at":null},{"created_at":null,"id":3,"language":"德语","updated_at":null},{"created_at":null,"id" :4,"language":"French","updated_at":null},{"created_at":null,"id":5,"language":"spanish","updated_at":null},{"created_at" :null,"id":6,"language":"dutch","updated_at":null},{"created_at":null,"id":7,"language":"Tamil","updated_at":null } ] }
更新
def index
@languages = Language.all
respond_to do |format|
format.json { render json: @languages}
end
end
更新 2
class Language < ActiveRecord::Base
ActiveRecord::Base.include_root_in_json = false
has_and_belongs_to_many :users
end