在另一个中包含另一个 jbuilder 模板
Including other one jbuilder template in another
考虑以下 show.json.jbuilder
模板:
json.user do |json|
json.extract! @user, :id, :username, :email, :created_at, :avatar
end
json.policies do |json|
json.array!(@policies) do |policy|
json.extract! policy, :id, :title, :cached_votes_score
end
end
json.liked do |json|
json.array!(@liked) do |policy|
json.extract! policy, :id, :title, :cached_votes_score
end
end
json.disliked do |json|
json.array!(@disliked) do |policy|
json.extract! policy, :id, :title, :cached_votes_score
end
end
是否可以将它们分成四个不同的模板,然后将它们包含在 show
模板中?类似于:
include template_user
include template_policies
include template_liked
include template_disliked
如果这不可能,有什么替代方法可以创建更模块化的 jbuilder 代码?我发现我的 jbuilder 代码非常不干。
是的。有可能。像这样拆分文件:
_user.json.jbuilder
json.user do |json|
json.extract! @user, :id, :username, :email, :created_at, :avatar
end
_policies.json.jbuilder
json.policies do |json|
json.array!(@policies) do |policy|
json.extract! policy, :id, :title, :cached_votes_score
end
end
_liked.json.jbuilder
json.liked do |json|
json.array!(@liked) do |policy|
json.extract! policy, :id, :title, :cached_votes_score
end
end
_disliked.json.jbuilder
json.disliked do |json|
json.array!(@disliked) do |policy|
json.extract! policy, :id, :title, :cached_votes_score
end
end
然后你可以简单地加入他们:
show.json.jbuilder
json.partial! 'user'
json.partial! 'policies'
json.partial! 'liked'
json.partial! 'disliked'
但我建议稍微改进一下并使用局部变量编写它们,因此:
_user.json.jbuilder
json.user do |json|
json.extract! user, :id, :username, :email, :created_at, :avatar
end
并且您将其作为参数传递给用户:
json.partial! 'user', user: @user
考虑以下 show.json.jbuilder
模板:
json.user do |json|
json.extract! @user, :id, :username, :email, :created_at, :avatar
end
json.policies do |json|
json.array!(@policies) do |policy|
json.extract! policy, :id, :title, :cached_votes_score
end
end
json.liked do |json|
json.array!(@liked) do |policy|
json.extract! policy, :id, :title, :cached_votes_score
end
end
json.disliked do |json|
json.array!(@disliked) do |policy|
json.extract! policy, :id, :title, :cached_votes_score
end
end
是否可以将它们分成四个不同的模板,然后将它们包含在 show
模板中?类似于:
include template_user
include template_policies
include template_liked
include template_disliked
如果这不可能,有什么替代方法可以创建更模块化的 jbuilder 代码?我发现我的 jbuilder 代码非常不干。
是的。有可能。像这样拆分文件:
_user.json.jbuilder
json.user do |json|
json.extract! @user, :id, :username, :email, :created_at, :avatar
end
_policies.json.jbuilder
json.policies do |json|
json.array!(@policies) do |policy|
json.extract! policy, :id, :title, :cached_votes_score
end
end
_liked.json.jbuilder
json.liked do |json|
json.array!(@liked) do |policy|
json.extract! policy, :id, :title, :cached_votes_score
end
end
_disliked.json.jbuilder
json.disliked do |json|
json.array!(@disliked) do |policy|
json.extract! policy, :id, :title, :cached_votes_score
end
end
然后你可以简单地加入他们:
show.json.jbuilder
json.partial! 'user'
json.partial! 'policies'
json.partial! 'liked'
json.partial! 'disliked'
但我建议稍微改进一下并使用局部变量编写它们,因此:
_user.json.jbuilder
json.user do |json|
json.extract! user, :id, :username, :email, :created_at, :avatar
end
并且您将其作为参数传递给用户:
json.partial! 'user', user: @user