我使用JBuilder作爲返回一些JSON。我有一個index.json.jbuilder
生成的數據,我需要將其呈現爲一個字符串。但是,我不知道如何做到這一點,因爲:@my_object.to_json
和@my_object.as_json
似乎沒有經過JBuilder。我怎樣才能呈現一個字符串JBuilder視圖的JSON表示?
如何將JBuilder視圖呈現爲字符串?
我使用JBuilder作爲返回一些JSON。我有一個index.json.jbuilder
生成的數據,我需要將其呈現爲一個字符串。但是,我不知道如何做到這一點,因爲:@my_object.to_json
和@my_object.as_json
似乎沒有經過JBuilder。我怎樣才能呈現一個字符串JBuilder視圖的JSON表示?
如何將JBuilder視圖呈現爲字符串?
我渲染用戶的集合作爲一個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
嗨,亞倫,我試圖實現上述技術,但當我去的網頁,我得到一個缺少模板錯誤:「缺少模板/work.json與{:語言環境=> [:恩],:格式=> [: html],:handlers => [:erb,:builder,:arb,:jbuilder,:coffee]}。「我對Rails有點新鮮感,不太清楚發生了什麼。任何幫助將非常感激。提前致謝! – jordancooperman 2013-03-27 02:47:16
我猜你可能需要在控制器中添加「respond_to:json」。 – 2013-03-27 03:00:25
我在我的控制器中仍然收到與「respond_to:json」相同的錯誤 – jordancooperman 2013-03-27 03:20:56
查看源代碼,它看起來像你可以這樣做:
json_string = Jbuilder.encode do |json|
json.partial! 'path/to/index', @my_object
end
你可以也可以這樣做,這會使控制器更清潔一些。
# 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>
如果視圖users.json.jbuilder
是相對於控制器默認路徑爲,它無法找到模板,它可能是由於format
差異,因爲它可能會試圖去尋找html
格式文件。有兩種方法來解決這個問題:
已在客戶端GET /users/index.json
或
調用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中驗證過。
我認爲這應該被評爲解決方案,因爲它適用於常見的情況,並且比選擇的解決方案容易得多 – 2015-06-05 08:43:41
任何在rails 4.1上的人都應該使用這個答案。 – Stone 2015-09-15 06:48:58
從控制檯:
view = ApplicationController.view_context_class.new("#{Rails.root}/app/views")
JbuilderTemplate.encode(view){|json| json.partial!('path/to/index', @my_object) }
通過https://github.com/rails/jbuilder/issues/84#issuecomment-38109709
如果你在控制器中這樣做,一個更簡單的方法是嘗試到移動代碼到視圖控制器所呈現。
我這裏所描述的: 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
在控制器,你可以像這樣
def index
json = JbuilderTemplate.new(view_context) do |json|
json.partial! 'index'
end.attributes!
do_something(json)
render json: json
end
請注意,您需要「_index.json」。。JBuilder的」,因爲它要求部分渲染
繼justingordon的尖端
如果您使用的是陣營組件,你可以做以下
在你的控制器:
@users = User.all
在你查看:
<%= react_component("YourComponentName",
props: render('your_template.json.jbuilder')) %>
這是在Rails 5.1上測試的。
您是否試圖從控制器返回JSON作爲JSON響應或者單獨構建JSON字符串? – Winfield 2012-04-18 20:15:46