我有3個控制器Rails應用程序渲染:呼叫從另一個控制器
categories
,subcategories
,all
的兩個第一取從DB數據,並經由
渲染它最後一個應該得到第一個控制器的結果,第二個控制器的結果並渲染它們。
簡單的例子:
# categories_controller.rb
class CategoriesController < ApplicationController
def index
render "list"
end
end
# subcategories_controller.rb
class SubcategoriesController < ApplicationController
def index
render "list"
end
end
# all_controller.rb
class AllController < ApplicationController
def index
# should render the both and join them
end
end
# list.html.erb
this is the list
結果:
/category
=> "this is the list "
/subcategory
=> "this is the list "
/all
=> "this is the list this is the list "
席力圖召
render "subcategory"
render "subcategory/index"
但似乎沒有任何工作。
你有什麼想法嗎?
感謝,
先前的做法是以下幾點:
# categories_controller.rb
class CategoriesController < ApplicationController
def self.getAll
return Category.all
end
def index
@data = CategoriesController.getAll
# some logic here
render "list"
end
end
# subcategories_controller.rb
class SubcategoriesController < ApplicationController
def self.getAll
return Category.all
end
def index
@data = SubcategoriesController.getAll
# some logic here
render "list"
end
end
# all_controller.rb
class AllController < ApplicationController
def index
@categoriesData = CategoriesController.getAll
@subcategoriesData = SubcategoriesController.getAll
render 'all'
end
end
# list.html.erb
<%= @data %>
# all.html.erb
<%= @categoriesData %>
<%= @subcategoriesData %>
但我不得不重寫已經存在邏輯的一部分...
你不能在控制器中渲染兩次,我不認爲。爲什麼不使用partials和subcategory,然後在所有控制器索引操作中將它們都渲染到視圖中? –
我嘗試使用'render_to_string'方法,然後合併這兩個。一個已經存在的部分,這個例子是爲了簡化問題,但如果你認爲它是值得的,我可以更新它。 – nobe4