【发布时间】:2015-07-31 00:49:48
【问题描述】:
我有一个带有 3 个控制器的 rails 应用程序:
categories、subcategories、all
两个first从db中获取数据并通过渲染
render "list"
最后一个应该得到第一个控制器的结果,第二个控制器的结果并渲染它们。
简化示例:
# 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 %>
但是我必须重写一部分已经存在的逻辑......
【问题讨论】:
-
你不能在控制器中渲染两次我不认为。为什么不对类别和子类别使用部分,然后在所有控制器索引操作的视图中呈现它们?
-
我尝试使用
render_to_string方法,然后将两者合并。部分已经存在,这个例子是为了简化问题,但如果你认为值得,我可以更新它。
标签: ruby-on-rails ruby controller render