I have a search box that I only want to render on particular pages through the navigation section in my application.html.erb file.
How do I set exceptions? Is this done through the main application controller?
I have a search box that I only want to render on particular pages through the navigation section in my application.html.erb file.
How do I set exceptions? Is this done through the main application controller?
有几种方法可以做到这一点。
最明显的方法是使用实例变量进行标记。
在你的application.html.erb
<%= render 'search' if @search_box %>
无论您想在哪里显示搜索,请将标志实例变量设置为 true。
def show
@search_box = true
...
end
编辑
如果您想要多个操作来显示搜索,您可能还想在控制器中使用 Rails 的过滤器。
before_action :flag_search_box, :only => [:show, :new, :all_kinds_of_controller_actions_where_i_wanna_show_search]
...
private
def flag_search_box
@search_box = true
end
我可能会建议不要将搜索框放在 application.html 页面中,而可能会放在您希望它呈现的单独的 html 页面中。您可以将搜索设为部分搜索,这样您只需一行代码就可以从其他页面访问它。