Tag: jbuilder

如何从JBuilder视图委托模型的to_builder方法?

假设我有一个Person类和一个Gang类 class Person belongs_to :gang attr_accessible :name, :secret def to_builder Jbuilder.new do |app| person.id id person.name name end end end class Gang has_many :people attr_accessible :name end 如何从视图中使用此to_builder方法? 例如 #app/views/gang/show.json.jbuilder (@gang set by the controller) json.gang do |json| json.name @gang.name json.gang_members(@gang.people) do |person| #how do I delegate to the person.to_builder here? end end 请注意,我不想只使用默认的Person.as_json,因为我不想在Person上呈现secret属性。 我尝试过的大部分内容最终都是渲染了Person.as_json,而不是Person.to_builder。

将GeoJSON文件读入Rails / Leaflet

我用Rails jBuilder创建了GeoJSON文件,并希望将其读入Leaflt。 如果我直接使用它,就像L.mapbox.featureLayer().loadURL(‘overview/overview_data.geojson’).addTo(map)但我想使用Leaflet.timeline并使用GeoJSON。 如果我把GeoJSON放在脚本中就像这样 let data = {“type”:”FeatureCollection”,”features”:[{“type”:”Feature”,”properties”:{“start”:1903,”end”:1908,”name”:”part of S. Toluca St. (26). and second block south gone”,”title”:”Was Brook before 1903 and became part of S. Toluca St. (26). and second block south gone until at least 1908.”},”geometry”:{“type”:”LineString”,”coordinates”:[[-118.25862396508458,34.06087254304104],[-118.25933206826451,34.05994816216629]]}},{“type”:”Feature”,”properties”:{“start”:1903,”end”:1928,”name”:”part of E. 40th Place”,”title”:”Was 37th St before 1903 and became part of E. 40th Place until at […]

HAML防止模板引擎呈现除HTML之外的任何内容

我正在使用Jbuilder(我也尝试使用Rabl)来渲染json。 当我尝试在我的应用程序中呈现jbuilder模板时,它会在layouts / application文件中呈现模板并将HTML作为JSON返回(请参阅layouts / application’中的行’): 在Github上骑控制器 Started GET “/random_photo.json” Processing by RidesController#random_photo as JSON >> Rendered rides/random_photo.json.jbuilder within layouts/application (0.3ms) Rendered shared/_banners_in_corners.haml (3.0ms) Rendered shared/_sign_in_and_out.haml (2.0ms) Rendered layouts/_navigation.haml (7.3ms) Completed 200 OK in 156ms (Views: 120.7ms | ActiveRecord: 3.1ms) 但是,当我在没有模板的情况下render json: @ride.as_json ,并执行render json: @ride.as_json调用时,事情按预期工作。 当我创建一个新的应用程序并尝试做同样的事情时,一切都按预期工作: Started GET “/posts/1.json” Processing by PostsController#show as JSON […]

JBuilder模板永远不会被调用

在我的Rails 4应用程序中,我有一个API::V1::ClustersController结构如下: class Api::V1::ClustersController < ApplicationController respond_to :json def index @clusters = Cluster.all render json: @clusters end class 在我的app/views/api/v1/clusters/index.json.jbuilder视图中: json.array!(@clusters) do |cluster| json.extract! cluster, :id, :index json.url cluster_url(cluster, format: :json) end 在我的路线: namespace :api, defaults: { format: :json } do namespace :v1 do authenticated :user do resources :clusters end end end 不幸的是,当我点击http://localhost:3000/api/v1/clusters.json时,以下是json输出: { clusters: [ […]

Rabl,Jbuilder还是用于api的手动json构建?

要为大规模应用程序构建api,哪种方法是更好的性能,我应该使用Rabl,Jbuilder还是手动构建json对象?我正在为移动应用程序构建api /端点。

使用Jbuilder(或其他)Rails JSON API布局

在我的rails 3.2应用程序中,我使用jbuilder来呈现来自我的JSON api的响应。 我想为所有API响应提供一个通用结构,并且布局可能是保持我的视图DRY的可能解决方案。 例如:我希望每个回复都是以下forms: { status: “ok|error|redirect”, data: { … JSON specific to the current view … }, errors: [ … ], notes: [ … ] } (其中数据的值是视图提供的json结构,其他所有内容都来自布局) 但是:我无法让jbuilder布局正确地产生视图。 # in layout json.data yield # in view json.some “value” 结果是: {“data”:”{\”some\”:\”value\”}”} # arg! my json has become a string 以另一种方式尝试: # in layout yield […]

用于模型属性的JBuilder动态键

我正在尝试构建一个JSON文件,以通过变体代码访问产品的变体。 理想情况下,我可以调用variations.abcdefgh,基本上要求定义这样的结构: { “variations”:{ “abcdefgh”:{ “available”:true, “price”:”12.00″ }, “ijklmnop”:{ “available”:false, “price”:”25.00″ } } } 在products / show.json.jbuilder中做一些事情 json.variations @product.variations 给我格式化如下: { “variations”:[ { “available”:true, “price”:”12.00″, “product_code”:”abcdefgh” }, { “available”:true, “price”:”25.00″, “product_code”:”ijklmnop” } ] } 也许我的JavaScript可能会更好,但我想要一些基于product_code更容易访问的东西。 我得到的最接近的是 @product.variations.each do |variation| json.set! variation.product_code, variation end 但我还没有找到一个解决方案,将这些正确格式化的对象作为子项放入变体属性中。 有没有人遇到过类似的事情?

如何使用Rails Jbuilder提取所有属性?

在jbuilder.json模板中一直编写这样的代码是一件痛苦的事: json.extract! notification, :id, :user_id, :notice_type, :message, :resource_type, :resource_id, :unread, :created_at, :updated_at 所以我想这样编码; json.extract_all! notification 我发现我可以像下面的代码那样做,但它们对我来说仍然有点冗长。 notification.attributes.each do |key, value| json.set!(key, value) end 有没有更好的方法?

使用JBuilder在rails中创建嵌套的JSON输出

我正在寻找有关如何使用JBuilder创建嵌套JSON输出的示例。 我想创建和输出类似于: { “name”: “John Doe”, “reservations”: [ { “restaurant”: “ABC”, “reservation_time”: “2012/12/01 20:00”, “details”: { “address”: “somewhere”, “rating”: “5” } }, { “restaurant”: “CDE”, “reservation_time”: “2012/12/04 20:00”, “details”: { “address”: “somewhere else”, “rating”: “3” } } ] }

我怎样才能将字符串呈现为JBuilder视图的JSON表示?

我正在使用JBuilder来返回一些JSON。 我有一个index.json.jbuilder来生成数据,我需要将它渲染为一个字符串。 但是,我不知道如何做到这一点,因为: @my_object.to_json和@my_object.as_json似乎没有通过JBuilder。 我怎么能将JBuilder视图呈现为字符串?