Ask A Question

Notifications

You’re not receiving notifications from this thread.

How do I route to the proper controller from the Bootstrap navbar?

Rodolphe Geant asked in Rails

Dear All,

My name is Rodolphe, I am a new member of Go Rails. I've fell I love with Rails a year ago! but gosh I so struggle sometimes to comprehend what I'm doing :( . Anyway here is my question.

I have a simple search form in my navbar that runs a search in the :address column of my Users table through the jobs_path (nested resources):

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 the routes 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 :users, only: [:show, :index]
resources :jobs
resources :users do
resources :jobs
end
end

The user model shows:
has_many :jobs, dependent: :destroy

And the Job model:
belongs_to :user

My Jobs controller shows:

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

rails routes shows:

         Prefix Verb                                URI Pattern                                  Controller#Action
    new_user_session GET          /users/sign_in(.:format)                     devise/sessions#new
        user_session POST            /users/sign_in(.:format)                     devise/sessions#create
 destroy_user_session DELETE /users/sign_out(.:format)                    devise/sessions#destroy
   new_user_password GET    /users/password/new(.:format)           devise/passwords#new
  edit_user_password GET    / users/password/edit(.:format)            devise/passwords#edit
       user_password PATCH     /users/password(.:format)                   devise/passwords#update
                     PUT                       /users/password(.:format)                  devise/passwords#update
                     POST                     /users/password(.:format)                 devise/passwords#create

cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
user_registration PATCH /users(.:format) devise/registrations#update
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
POST /users(.:format) devise/registrations#create
new_user_confirmation GET /users/confirmation/new(.:format) devise/confirmations#new
user_confirmation GET / users/confirmation(.:format) devise/confirmations#show
POST /users/confirmation(.:format) devise/confirmations#create
new_user_unlock GET /users/unlock/new(.:format) devise/unlocks#new
user_unlock GET /users/unlock(.:format) devise/unlocks#show
POST /users/unlock(.:format) devise/unlocks#create
static_pages_home GET /static_pages/home(.:format) static_pages#home
static_pages_faq GET /static_pages/faq(.:format) static_pages#faq
static_pages_about GET /static_pages/about(.:format) static_pages#about
static_pages_cgu GET /static_pages/cgu(.:format) static_pages#cgu
root GET / static_pages#home
faq GET /faq(.:format) static_pages#faq
about GET /about(.:format) static_pages#about
contact GET /contact(.:format) static_pages#contact
cgu GET /cgu(.:format) static_pages#cgu
signup GET /signup(.:format) users#new
login GET /login(.:format) sessions#new
POST /login(.:format) sessions#create
logout DELETE /logout(.:format) sessions#destroy
users GET /users(.:format) users#index
user GET /users/:id(.:format) users#show
jobs GET /jobs(.:format) jobs#index
POST /jobs(.:format) jobs#create
new_job GET /jobs/new(.:format) jobs#new
edit_job GET /jobs/:id/edit(.:format) jobs#edit
job GET /jobs/:id(.:format) jobs#show
PATCH /jobs/:id(.:format) jobs#update
PUT /jobs/:id(.:format) jobs#update
DELETE /jobs/:id(.:format) jobs#destroy
user_jobs GET /users/:user_id/jobs(.:format) jobs#index
POST /users/:user_id/jobs(.:format) jobs#create
new_user_job GET /users/:user_id/jobs/new(.:format) jobs#new
edit_user_job GET /users/:user_id/jobs/:id/edit(.:format) jobs#edit
user_job GET /users/:user_id/jobs/:id(.:format) jobs#show
PATCH /users/:user_id/jobs/:id(.:format) jobs#update
PUT /users/:user_id/jobs/:id(.:format) jobs#update
DELETE /users/:user_id/jobs/:id(.:format) jobs#destroy
GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
GET /users/:id(.:format) users#show
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy

Yes, the jobs: route is there twice as I need to access the Jobs actions for visitors (outside of Devise).

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" for 127.0.0.1 at 2017-11-16 10:47:07 +0400
            Processing by JobsController#index as HTML
            Rendering jobs/index.html.erb within layouts/application
            (0.4ms)  SELECT COUNT(*) FROM "jobs"
            Job Load (0.4ms)  SELECT  "jobs".* FROM "jobs" ORDER BY id DESC LIMIT $1 .......................

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-16 12:11:48 +0400
            Processing by StaticPagesController#home as HTML
            Parameters: {"utf8"=>"✓", "address"=>"Nantes", "commit"=>"Rechercher"} ..................

How do I make sure the query is always sent to the correct controller, regardless of where I am?
Sorry for the looong question!
I hope someone has a solution....Thank you in advance

Reply

I think the routes are confusing the system.

I'd do a namespace for your users.

namespace :users do 
    resources :jobs
end

And you can keep your original jobs path for external users.

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 86,563+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.