2012-04-13 28 views
21

我使用JBuilder作爲返回一些JSON。我有一個index.json.jbuilder生成的數據,我需要將其呈現爲一個字符串。但是,我不知道如何做到這一點,因爲:@my_object.to_json@my_object.as_json似乎沒有經過JBuilder。我怎樣才能呈現一個字符串JBuilder視圖的JSON表示?

如何將JBuilder視圖呈現爲字符串?

+0

您是否試圖從控制器返回JSON作爲JSON響應或者單獨構建JSON字符串? – Winfield 2012-04-18 20:15:46

回答

39

我渲染用戶的集合作爲一個JSON字符串在控制器中,像這樣:

#controllers/users_controller.rb 
def index 
    @users = User.all 
    @users_json = render_to_string(template: 'users.json.jbuilder', locals: { users: @users}) 
end 

#views/users/users.json.jbuilder 
json.array!(users) do |json, user| 
    json.(user, :id, :name) 
end 
+0

嗨,亞倫,我試圖實現上述技術,但當我去的網頁,我得到一個缺少模板錯誤:「缺少模板/work.json與{:語言環境=> [:恩],:格式=> [: html],:handlers => [:erb,:builder,:arb,:jbuilder,:coffee]}。「我對Rails有點新鮮感,不太清楚發生了什麼。任何幫助將非常感激。提前致謝! – jordancooperman 2013-03-27 02:47:16

+0

我猜你可能需要在控制器中添加「respond_to:json」。 – 2013-03-27 03:00:25

+0

我在我的控制器中仍然收到與「respond_to:json」相同的錯誤 – jordancooperman 2013-03-27 03:20:56

2

查看源代碼,它看起來像你可以這樣做:

json_string = Jbuilder.encode do |json| 
    json.partial! 'path/to/index', @my_object 
end 
+0

我嘗試了大約20分鐘的時間來完成它,沒有多少運氣。如果沒有太多要問,你可以發佈一個這樣的工作例子嗎? – Geo 2012-04-19 11:12:59

+0

我從文檔中的Jbuilder示例中提取了這段代碼片段:https://github.com/rails/jbuilder 它看起來像嵌入式模板可能在Ruby 1.8下被破壞,只能在Ruby 1.9和更高版本上運行。 – Winfield 2012-04-19 18:26:26

+0

這對我不起作用。 – maletor 2014-03-10 21:32:25

3

你可以也可以這樣做,這會使控制器更清潔一些。

# controller 
def new 
    @data = Data.all 
end 


# view 
<% content_for :head do %> 
    <script type="text/javascript"> 
    var mydata = <%= raw render :partial => 'path/to/partial', :locals => {data: @data} %>; 
    </script> 
<% end %> 


# path/to/_partial.html.jbuilder 
json.array!(@data) do |d| 
    json.extract! field1, :field2, :field3, :field4 
    json.url data_url(d, format: :json) 
end 


# layouts/application.html 
<!DOCTYPE html> 
<html> 
<head> 
    <%= yield :head %> 
</head> 
<body> 
... 
</body> 
</html> 
+0

我更喜歡這種方法,也可以通過從擴展中刪除格式部分來使其格式不可知'_partial.jbuilder'可以在json和html視圖中使用。 – wik 2014-01-06 21:53:23

+0

這種方法很好,但它在'content_tag'的數據屬性中不起作用。是什麼賦予了? – maletor 2014-03-10 21:28:38

8

如果視圖users.json.jbuilder是相對於控制器默認路徑爲,它無法找到模板,它可能是由於format差異,因爲它可能會試圖去尋找html格式文件。有兩種方法來解決這個問題:

  1. 已在客戶端GET /users/index.json

  2. 調用render_to_string時,指定formats選項(也適用於render):


#controllers/users_controller.rb 
def index 
    @users = User.all 
    @users_json = render_to_string(formats: 'json') # Yes formats is plural 
end 

這已經在Rails 4.1中驗證過。

+0

我認爲這應該被評爲解決方案,因爲它適用於常見的情況,並且比選擇的解決方案容易得多 – 2015-06-05 08:43:41

+2

任何在rails 4.1上的人都應該使用這個答案。 – Stone 2015-09-15 06:48:58

2

如果你在控制器中這樣做,一個更簡單的方法是嘗試到移動代碼到視圖控制器所呈現。

我這裏所描述的: https://github.com/shakacode/react-webpack-rails-tutorial#jbuilder-notes

基本上你可以調用render在視圖中,就大功告成了。就像這樣:

<%= react_component('App', render(template: "/comments/index.json.jbuilder"), 
    generator_function: true, prerender: true) %> 

這裏有一個,如果你想從控制器到視圖通過數據會發生什麼情況的說明:

class PagesController < ApplicationController 
    def index 
    @comments = Comment.all 

    # NOTE: The below notes apply if you want to set the value of the props in the controller, as 
    # compared to he view. However, it's more convenient to use Jbuilder from the view. See 
    # app/views/pages/index.html.erb:20 
    # 
    # <%= react_component('App', render(template: "/comments/index.json.jbuilder"), 
    #  generator_function: true, prerender: true) %> 
    # 
    # 
    # NOTE: this could be an alternate syntax if you wanted to pass comments as a variable to a partial 
    # @comments_json_sting = render_to_string(partial: "/comments/comments.json.jbuilder", 
    #           locals: { comments: Comment.all }, format: :json) 
    # NOTE: @comments is used by the render_to_string call 
    # @comments_json_string = render_to_string("/comments/index.json.jbuilder") 
    # NOTE: It's CRITICAL to call respond_to after calling render_to_string, or else Rails will 
    # not render the HTML version of the index page properly. (not a problem if you do this in the view) 
    # respond_to do |format| 
    # format.html 
    # end 
    end 
end 
0

在控制器,你可以像這樣

def index 
    json = JbuilderTemplate.new(view_context) do |json| 
    json.partial! 'index' 
    end.attributes! 
    do_something(json) 
    render json: json 
end 

請注意,您需要「_index.json」。。JBuilder的」,因爲它要求部分渲染

0

繼justingordon的尖端

如果您使用的是陣營組件,你可以做以下

在你的控制器:

@users = User.all 

在你查看:

<%= react_component("YourComponentName", 
        props: render('your_template.json.jbuilder')) %> 

這是在Rails 5.1上測試的。

相關問題