简体   繁体   English

如何:使用完全相同的Rabl模板来Rails response_to json?

[英]how to: Rails respond_to json using exact same rabl template?

I am have two actions of which renders eventually should output json using the same rabl template, but at the moment, they each have a template with their own name 我有两个动作,它们的渲染最终应该使用相同的rabl模板输出json,但是目前,它们每个都有一个带有自己名称的模板

dashboard.json.rabl and batch_create.json.rabl dashboard.json.rabl和batch_create.json.rabl

they are the exact same, how can I specify in the batch_create template to use the dashboard's template? 它们是完全一样的,如何在batch_create模板中指定使用仪表板的模板?

Thank you! 谢谢!

EDIT #including controller's two actions and their rabl views 编辑#包括控制器的两个动作及其Rabl视图

line_items_controller.rb line_items_controller.rb

def dashboard
  @line_items ||= LineItem.limit(1000).all.to_a
  pids = [1,2,3,4,5]
  @projects = Project.only(:id, :name).where(:_id.in => pids).to_a
  @users    = User.only(:first, :last, :role, :company_id).all.to_a
  @companies= Company.where(:_id.in => @users.map(&:company_id)).to_a
  @specs    = Spec.where(:specable_id.in => pids).to_a
    spec_ids= @specs.map { |e| e.id }
  @contact_infos = ContactInfo.where(:infoable_id.in => spec_ids).to_a    end
  gon.rabl

  respond_to do |format|
    format.html
    format.json
  end
end

def batch_create
  @line_items = LineItem.where(:_id.in => params[:line_item_ids]).to_a

  # else same as #dashboard
end

app/views/line_items/dashboard.json.rabl SAME AS app/views/line_items/batch_create.json.rabl app / views / line_items / dashboard.json.rabl相同的app / views / line_items / batch_create.json.rabl

object false

child @projects => :projects do
  attributes :id, :name
end

child @companies => :companies do
  attributes :id, :name
end

child @users => :users do
  attributes :id, :full, :company_id
end

child @specs => :specs do
  attributes :id, :style, :due_at_note
end

child @contact_infos => :contact_infos do
    attributes :info, :infoable_id
end

child @line_items do
  attributes :id, :title, :dashboard_length, :dashboard_created_at, :original_file_url, :project_id
end

您是否尝试在batch_create.json.rabl中使用它:

extends '<insert_object_name>/dashboard'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM