chickensmitten
chickensmitten

Reputation: 495

form_tag, controller, action not redirecting to posts/index

this should be easy, but I am not sure why it does not work. I have a search form in the nav bar. When I conduct search in the navbar in posts/index, I can get search results with no problem. As shown below:

http://localhost:3000/posts?utf8=✓&query=technology

However, when I go to posts/show and conduct a search, the search does not redirect me to posts/index to provide results. Instead I get the below.

http://localhost:3000/categories/history?utf8=✓&query=technology.

The output from pry.

[1] pry(#<CategoriesController>)> params
=> {"utf8"=>"✓", "query"=>"technology", "controller"=>"categories", "action"=>"show", "id"=>"history"}

Code is as below:

Views/layouts/_navigation.html.erb

<%= form_tag({:controller => "posts", :action => "index"}, method: :get) do %>        
<div class="form-group">
  <%= text_field_tag :query, params[:query], class: "form-control", placeholder: "Search Here" %>
</div>
  <%= submit_tag "Search", name: nil, class: "btn btn-default" %>
<% end %> 

routes.rb

resources :posts 
resources :categories

Thanks in advance.

UPDATE 1

1) Adding picture from Safari inspect element enter image description here

2) I also made a mistake by saying that "...when I go to posts/show and conduct..." it should be "...when I go to categories/show and conduct..."

UPDATE 2

Per request from the comment, here are some additional information for consideration.

Views/layouts/_navigation.html.erb

  <li>
  <form class="navbar-form navbar-left">
      <%= form_tag({:controller => "posts", :action => "index"}, method: :get) do %>        
    <div class="form-group">
      <%= text_field_tag :query, params[:query], class: "form-control", placeholder: "Search This Page" %>
    </div>
      <%= submit_tag "Search", name: nil, class: "btn btn-default" %>
    <% end %>        
  </form>
  </li> 

views/categories/show.html.erb

<div id="wrapper">
  <div id="columns">
    <% @category.each do |post| %>
      <%= render 'posts/post', post: post %>
    <% end %>
  </div>
</div>

<%= will_paginate @category %>

views/posts/show.html.erb

<div id="wrapper">
  <div id="columns">
    <% @posts.each do |post| %>
      <%= render 'post', post: post %>
    <% end %>
  </div>
</div>


<%= will_paginate @posts %>

Also, please find additional screenshots of posts/index and categories/show attached below.

enter image description here enter image description here

Upvotes: 1

Views: 886

Answers (1)

hahcho
hahcho

Reputation: 1409

This is your problem:

<form class="navbar-form navbar-left">
  <%= form_tag({:controller => "posts", :action => "index"}, method: :get) do

form_tag will generate <form> element for you. Now you have nesting of form elements and the top one is considered to be the form. Remove <form class="navbar-form navbar-left"> and add to form_tag :class => 'navbar-form navbar-left' as another option.

Upvotes: 3

Related Questions