Katie H
Katie H

Reputation: 2293

Rendering on specific pages through application.html.erb file?

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?

Upvotes: 3

Views: 752

Answers (2)

Jason Kim
Jason Kim

Reputation: 19031

There are several ways to do this.

Most obvious way is to use an instance variable for flagging.

In your application.html.erb

<%= render 'search' if @search_box %>

And wherever you want to show the search, set the flag instance variable to true.

def show
  @search_box = true
  ...
end

Edit

You might also want to utilize Rails' filters in your controllers if you want multiple actions to show search.

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

Upvotes: 2

challeng
challeng

Reputation: 175

I might suggest to put the search box not in the application.html page but maybe in the separate html pages that you want it to render on. You could make the search a partial so that you could access it from the other pages with just one line of code.

Upvotes: 0

Related Questions