我正在尝试呈现自定义部分(如果存在),因为我正在使用一个代码库运行多个网站。视图通常是相同的,但有时它们不同,以便更容易呈现自定义视图。
有谁知道如何通过下面的方法调用渲染?我遇到的问题是你必须在调用render之后调用return,但是我必须在“index”方法中调用return而不是在“render_custom_view_if_exists”方法中调用return。我似乎无法找到一种聪明的方法来自动检查视图是否存在并呈现它,因为我必须手动进入每个方法来调用返回以取消呈现默认视图。
class ApplicationController
after_action :render_custom_view_if_exists
def render_custom_view_if_exists
render "#{path}" if lookup_context.template_exists?("#{path}")
return true
end
end
class UsersController < ApplicationController
def index
@users = User.all
end
end
ERROR: Render and/or redirect were called multiple times in this action.
Please note that you may only call render OR redirect, and at most once per action.
Also note that neither redirect nor render terminate execution of the action,
so if you want to exit an action after redirecting, you need to do something like "redirect_to(...) and return".