Reputation: 371
Hello I want to show the search form inside the navbar only in the index page of products and the show page of the product. The thing is that it is showing on both the index page and the show page but when I click home to go to the root_path I get the following error:
No route matches {:action=>"show", :controller=>"products"} missing required keys: [:id]
How can I avoid that? This is my code in the application.html.erb:
<% if current_page?(products_path) || (product_path) %>
<div class="col-sm-3 col-md-3 pull-right">
<form class="navbar-form" role="search">
<div class="input-group">
<input type="text" class="form-control" placeholder="Search" name="search_term" id="search_term">
<div class="input-group-btn">
<button class="btn btn-default" type="submit"><i class="glyphicon glyphicon-search"></i></button>
</div>
</div>
</form>
</div>
<% end %>
Upvotes: 2
Views: 259
Reputation: 14910
It would be better to just check the current controller.
<% if controller.controller_name == 'products' %>
You also have access to action_name
if you wanna use that too
<% if controller.controller_name == 'products' && controller.action_name == 'show' %>
Upvotes: 2
Reputation: 1484
You can check for the product controller and action in order to achieve that. Checkout the below code.
if controller.controller_name == 'products' && %w(index show).include?(action_name)
For the action name in different ruby version have a look at Rails - controller action name to string
This will check for the product controller and both actions.
Upvotes: 0