Search form not routing to proper controller in Rails 5.1

独自空忆成欢 提交于 2020-01-15 09:09:22

问题


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:

<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>

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

resources 'contacts', only: [:new, :create], path_names: { new: '' }

if Rails.env.development?
mount LetterOpenerWeb::Engine, at: "/letter_opener"
end

end

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

Part of my 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"]]
Job Load (0.6ms)  SELECT  "jobs".* FROM "jobs" INNER JOIN "users" ON 
"users"."id" = "jobs"."user_id" WHERE "users"."address" = $1 LIMIT $2 OFFSET 
 $3  [["address", "nantes"], ["LIMIT", 3], ["OFFSET", 0]]
 User Load (0.3ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 
LIMIT $2  [["id", 1], ["LIMIT", 1]]
CACHE User Load (0.0ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = 
$1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
Rendered jobs/index.html.erb within layouts/application (43.9ms)
Rendered layouts/_header_out.html.erb (1.8ms)
Rendered layouts/_messages.html.erb (0.5ms)
Rendered layouts/_footer.html.erb (0.6ms)
Completed 200 OK in 119ms (Views: 101.0ms | ActiveRecord: 7.1ms)

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.


回答1:


I can see a redundant form tag at the top of your provided form snippet. Using Simple Form you just have to use the helper it provides:

<%= simple_form_for @user do |f| %>
  <%= f.input :username %>
  <%= f.input :password %>
  <%= f.button :submit %>
<% end %>

This will generate an entire form with labels for user name and password as well, and render errors by default when you render the form with invalid data (after submitting for example).

So please remove the first line of the form in the following snippet:

<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>

and rewrite it like:

<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>


来源:https://stackoverflow.com/questions/47385188/search-form-not-routing-to-proper-controller-in-rails-5-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!