gpt4 book ai didi

ruby-on-rails - 在同一个 Controller 中使用两个不同的布局模板?

转载 作者:行者123 更新时间:2023-12-03 18:42:14 26 4
gpt4 key购买 nike

我有一个指定了布局“pages.html.erb”的 PagesController。

  class PagesController < Spree::StoreController
layout 'pages'
respond_to :html

def lifestyle
render 'lifestyle'
end

def co_step_1
render 'co_step_1'
end

def co_step_2
render 'co_step_2'
end

end

是否可以在 PagesController 中使用其他方法来使用不同的布局?
换句话说,我想在其他方法中覆盖 layout 'pages.html.erb'

最佳答案

与其他人的答案略有不同。不需要 before Action 或类似 Action ,只需使用 layout 和方法来区分要使用的布局,例如:

class PagesController < Spree::StoreController

layout :resolve_layout
respond_to :html

def lifestyle
render 'lifestyle'
end

def co_step_1
render 'co_step_1'
end

def co_step_2
render 'co_step_2'
end

private

def resolve_layout
action_name == 'pages' ? 'pages' : 'custom_layout'
end

end

或者你想用什么逻辑来决定使用哪种布局。

关于ruby-on-rails - 在同一个 Controller 中使用两个不同的布局模板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36428293/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com