How do I add a controller-specific menu开发者_运维知识库 (or div) inside a general application layout in Rails?
method 1: set a variable in that controller
class SomeController
  before_filter :set_to_use_menu
  private
  def set_to_use_menu
    @use_menu = true
  end
end
method 2: determine the controller's name in the layout
<%- if controller_name == "your_controller_name" %>
  <%= render :partial => "the_menu" %>
<%- end %>
If I have correctly understood the question you needs to special place in layout.
Use <%= yield(:) %> in desired position in layout, for example:
    # 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 %>
See more in http://api.rubyonrails.org/classes/ActionView/Helpers/CaptureHelper.html#method-i-content_for
Just call an appropriately named partial in your layout
<%= render :partial => "#{controller_name}_menu" %>
If your controller is WidgetsController, then this will use the partial _widgets_menu.html.erb under ./app/views/layouts
Other than the content_for approach (which may or may not be what you want), there are a few additional options.
You could use a before_filter in your controller to set a variable:
# 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
Or, you could leave the controller alone and just check for the controller name in your layout:
# Layout
if controller.controller_name == 'test'
  # Do the controller-specific stuff you want to do
end
                                        
                                        
                                        
                                        
                                        
                                        
                                        
                                        
 加载中,请稍侯......
      
精彩评论