0
我有這兩個動作,最終使得使用相同的Rabl的模板應輸出JSON,但此刻,他們每個人都有自己的名字如何:Rails respond_to json使用完全相同的rabl模板?
dashboard.json.rabl和batch_create.json.rabl
模板他們是完全一樣的,我怎樣才能在batch_create模板中指定使用儀表板的模板?
謝謝!
編輯#包括控制器的兩個動作及其Rabl的意見
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
應用程序/視圖/ line_items/dashboard.json.rabl SAME AS應用程序/視圖/ 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
嗨TC,我試過,但但返回的對象被包裹在一個數組中。因此,在我的dashboard.json.rabl中,它會返回{「foo」:「bar」},如果我在batch_create.json.rabl中執行擴展'line_items/dashboard',我會返回[{「foo」:「bar 「}]。任何你知道如何排除它被視爲一個對象數組的機會?謝謝 – 2012-07-15 21:57:16
你能否用rabl視圖和控制器動作更新這個問題? – 2012-07-16 02:12:29
已添加!謝謝T C – 2012-07-17 00:08:34