Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/52.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails:布局中特定于控制器的菜单_Ruby On Rails_Ruby On Rails 3 - Fatal编程技术网

Ruby on rails Rails:布局中特定于控制器的菜单

Ruby on rails Rails:布局中特定于控制器的菜单,ruby-on-rails,ruby-on-rails-3,Ruby On Rails,Ruby On Rails 3,如何在Rails的一般应用程序布局中添加控制器特定的菜单(或div)?如果我正确理解了您需要在布局中放置特殊位置的问题 在布局中的所需位置使用,例如: # application.html.erb <%= yield(:right_menu) %> # show.html.erb <% content_for :right_menu do %> <!-- Everything in this block will be sh

如何在Rails的一般应用程序布局中添加控制器特定的菜单(或div)?

如果我正确理解了您需要在布局中放置特殊位置的问题

在布局中的所需位置使用,例如:

    # application.html.erb
    <%= yield(:right_menu) %>

    # show.html.erb
    <% content_for :right_menu do %>
    <!-- Everything in this block will be shown at the position yield(:right_menu) in layout -->
    <% end %>
#application.html.erb
#show.html.erb
在中查看更多信息
方法1:在该控制器中设置一个变量

class SomeController
  before_filter :set_to_use_menu

  private

  def set_to_use_menu
    @use_menu = true
  end
end
方法2:确定布局中控制器的名称

<%- if controller_name == "your_controller_name" %>
  <%= render :partial => "the_menu" %>
<%- end %>

“菜单”%>

除了方法的内容(可能是也可能不是您想要的),还有一些附加选项

您可以在控制器中使用before_过滤器设置变量:

# Controller
class TestController < ApplicationController
  before_filter :set_variable

  def set_variable
    @my_variable = true
  end
end

# Layout
if @my_variable
  # Do the controller-specific stuff you want to do
end

只需在布局中调用适当命名的分部

<%= render :partial => "#{controller_name}_menu" %>
“#{controller_name}_菜单”%>

如果您的控制器是
WidgetsController
,则这将使用
/app/views/layouts

下的部分
\u widgets\u menu.html.erb
,谢谢,但这是控制器操作特定的,而不是控制器特定的。我必须在控制器渲染的每个视图中添加内容,这是@use\u菜单的目的,例如,它在哪里使用?谢谢,我想我会使用你第一种方法的修改版本。@michael:我相信彼得旺想在布局中使用@use_menu来调节菜单HTMLW的呈现。当第二种方法较短并且只需要你在布局文件中添加代码时,为什么有人会考虑第一种方法?
<%= render :partial => "#{controller_name}_menu" %>