← Back to Upcase

Navbar search form not routing to proper controller in Rails 5.1


(Rodolphe Geant) #1

I’ve been struggling for few days on this one:

I have a search form in my navbar that runs a search in the :address column of my Users table through the job model. Here is the index method of my JobsController:

def index
@jobs = if params[:address]
Job.joins(:user).where(users: { address: params[:address].downcase
}).paginate(page: params[:page], per_page: 3)
else
@jobs = Job.paginate(page: params[:page], per_page: 3).order(‘id DESC’)
end
end

Here is the form:

<%= simple_form_for(jobs_path, method: :get) do %> <%= text_field_tag :address, params[:address], placeholder: " Votre Ville..." %> <%= submit_tag 'Rechercher', class:'btn btn-default' %> <% end %>

Here is my route file:

Rails.application.routes.draw do
devise_for :users

get ‘static_pages/home’

get ‘static_pages/faq’

get ‘static_pages/about’

get ‘static_pages/cgu’

root ‘static_pages#home’

get ‘/faq’, to: 'static_pages#faq’
get ‘/about’, to: 'static_pages#about’
get ‘/contact’, to: 'static_pages#contact’
get ‘/cgu’, to: 'static_pages#cgu’
get ‘/signup’, to: 'users#new’
get ‘/login’, to: 'sessions#new’
post ‘/login’, to: 'sessions#create’
delete ‘/logout’, to: ‘sessions#destroy’

resources :jobs, only: [:show, :index]
resources :users do
resources :jobs
end

The Jobs route is there twice as I need to make the index and view actions available for non logged in users / visitors.

Rails routes shows:

My problem is, that the request gets lost in routes and sometimes goes to the wrong controller. If I’m on the Jobs#index page : http://localhost:3000/jobs and run a search for a city like “Nantes”. The query runs fine:

Started GET “/jobs?utf8=%E2%9C%93&address=Nantes&commit=Rechercher” for
127.0.0.1 at 2017-11-20 08:34:48 +0400
Processing by JobsController#index as HTML
Parameters: {“utf8”=>“✓”, “address”=>“Nantes”, “commit”=>“Rechercher”}
Rendering jobs/index.html.erb within layouts/application
(0.4ms) SELECT COUNT(*) FROM “jobs” INNER JOIN “users” ON “users”.“id” =
“jobs”.“user_id” WHERE “users”.“address” = $1 [[“address”, “nantes”]] … … …

If I’m running the query from my root page ie: StaticPagesController, the query is sent to StaticPagesController and NOT to JobsController:

Started GET “/?utf8=%E2%9C%93&address=Nantes&commit=Rechercher” for 127.0.0.1
at 2017-11-20 08:37:49 +0400
Processing by StaticPagesController#home as HTML
Parameters: {“utf8”=>“✓”, “address”=>“Nantes”, “commit”=>“Rechercher”}
Rendering static_pages/home.html.erb within layouts/application

How do I make sure the query is always sent to the correct controller, regardless of where I am? Nothing that I’ve tried in the form seems to work! Thank you guys.


(Bruce) #2

Hmm…Can you post the html generated form code for the search bar on both pages? Is it the same? Or do you have a sample repo that duplicates the behavior?


(Rodolphe Geant) #3

Hello Bruce,

I appreciate your interest for this question. The live app can be seen at the following url:

Looking at the html of the generated form is something I did’t think of doing. From the inspector I get:

  form class="navbar-form navbar-left" role="search"
    <div class="form-group">
      <input name="utf8" type="hidden" value="✓">
      <input type="text" name="city" id="city" placeholder=" Votre Ville...">
      <input type="submit" name="commit" value="Rechercher" class="btn btn-default" data-disable-
       with="Rechercher">
    </div>

Does the “role=“search”” tag mean I have to create a search method in my controller somewhere?

Many thanks indeed for any hint you might have.

Rodolphe


(Rodolphe Geant) #4

Hello again Bruce.

I finally found someone on StackOverflow who spotted the issue. The issue was with my form tag. It was redundant:

 form class="navbar-form navbar-left">
 <div class="form-group">
 <%= simple_form_for(jobs_path, method: :get) do %>
 <%= text_field_tag :address, params[:address], placeholder: "Votre Ville..." %>
 <%= submit_tag 'Rechercher', class:'btn btn-default' %>
 <% end %>
 </div>

I’ve changed it to this:

 <div class="form-group navbar-form navbar-left">
 <%= simple_form_for(jobs_path, method: :get) do %>
 <%= text_field_tag :address, params[:address], placeholder: "Votre Ville..." %>
 <%= submit_tag 'Rechercher', class:'btn btn-default' %>
 <% end %>
 </div>

And it is working fine now. :slight_smile:


(Bruce) #5

Hooray, glad to hear it @rodolphe85!