在Ruby on Rails中基于条件渲染部分模板

时间:2022-03-08 00:11:43

I have 2-column layout. Some controller has left column, some not. What is the best way to render it depend on controller? Now, it look likes that:

我有2列布局。有些控制器已离开列,有些则没有。渲染它的最佳方法是什么依赖于控制器?现在,它看起来像这样:

<% if params[:controller] != 'page' %>
        <div id="navigation" class="l"><%= render "layouts/left-menu" %></div>
    <% end  %>

It's bad, bad monkey code.

这是糟糕的,糟糕的猴子代码。

2 个解决方案

#1


2  

Edit: Changed my solution, OP wanted condition to depend on action and controller.

编辑:更改了我的解决方案,OP希望条件依赖于操作和控制器。

In your base helper, define a method like this:

在您的基本帮助器中,定义如下方法:

# app/helpers/application_helper.rb
module ApplicationHelper
  def has_left_menu?
    @has_left_menu.nil? ? 
      true : # <= default, change to preference
      @has_left_menu
  end
end

In your application controller:

在您的应用程序控制器

# app/controllers/application_controller.rb
class ApplicationController
  def enable_left_menu!
    @has_left_menu = true
  end
  def disable_left_menu!
    @has_left_menu = false
  end
end

In your view or layout, change your check to this:

在您的视图或布局中,将支票更改为:

<% if has_left_menu? %>
    <div id="navigation" class="l"><%= render "layouts/left-menu" %></div>
<% end  %>

Now you can disable/enable the left menu in before_filters or anywhere else in your action:

现在,您可以在before_filters或操作中的任何其他位置禁用/启用左侧菜单:

class UsersController < ApplicationController
  # enable left menu for "index" action in this controller
  before_filter :enable_left_menu!, :only => [:index]

  # disable left menu for all actions in this controller
  before_filter :disable_left_menu!

  def index
    # dynamic left menu status based on some logic
    disable_left_menu! if params[:left_menu] == 'false'
  end
end

#2


1  

In your controller you use layout like this

在您的控制器中,您使用这样的布局

#PublicController is just an example
class PublicController < ApplicationController
  layout "left-menu"
end

And in the views/layouts folder you put the left-menu.html.erb

在views / layouts文件夹中放置left-menu.html.erb

with a stylesheet_link_tag to your spesific css file

使用stylesheet_link_tag到你的特定css文件

<%= stylesheet_link_tag 'left-menu' %>

You can learn more at rails guides

您可以在rails指南中了解更多信息

#1


2  

Edit: Changed my solution, OP wanted condition to depend on action and controller.

编辑:更改了我的解决方案,OP希望条件依赖于操作和控制器。

In your base helper, define a method like this:

在您的基本帮助器中,定义如下方法:

# app/helpers/application_helper.rb
module ApplicationHelper
  def has_left_menu?
    @has_left_menu.nil? ? 
      true : # <= default, change to preference
      @has_left_menu
  end
end

In your application controller:

在您的应用程序控制器

# app/controllers/application_controller.rb
class ApplicationController
  def enable_left_menu!
    @has_left_menu = true
  end
  def disable_left_menu!
    @has_left_menu = false
  end
end

In your view or layout, change your check to this:

在您的视图或布局中,将支票更改为:

<% if has_left_menu? %>
    <div id="navigation" class="l"><%= render "layouts/left-menu" %></div>
<% end  %>

Now you can disable/enable the left menu in before_filters or anywhere else in your action:

现在,您可以在before_filters或操作中的任何其他位置禁用/启用左侧菜单:

class UsersController < ApplicationController
  # enable left menu for "index" action in this controller
  before_filter :enable_left_menu!, :only => [:index]

  # disable left menu for all actions in this controller
  before_filter :disable_left_menu!

  def index
    # dynamic left menu status based on some logic
    disable_left_menu! if params[:left_menu] == 'false'
  end
end

#2


1  

In your controller you use layout like this

在您的控制器中,您使用这样的布局

#PublicController is just an example
class PublicController < ApplicationController
  layout "left-menu"
end

And in the views/layouts folder you put the left-menu.html.erb

在views / layouts文件夹中放置left-menu.html.erb

with a stylesheet_link_tag to your spesific css file

使用stylesheet_link_tag到你的特定css文件

<%= stylesheet_link_tag 'left-menu' %>

You can learn more at rails guides

您可以在rails指南中了解更多信息