搜索表单未路由到Rails 5.1中的适当控制器 [英] Search form not routing to proper controller in Rails 5.1

查看:80
本文介绍了搜索表单未路由到Rails 5.1中的适当控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为这几天苦苦挣扎了:

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

我的导航栏中有一个搜索表单,该表单在用户的:address列中运行搜索表通过工作模型。这是我的JobsController的索引方法:

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

以下是形式:

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

这是我的路线文件:

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

结束

乔布斯路线存在两次,因为我需要创建索引并查看可用于未登录用户/访问者的操作

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:

我的问题是,请求丢失了在路线上,有时会走错控制器。如果我在Jobs#index页面上: http:// localhost:3000 / jobs 并运行搜索像南特这样的城市。查询运行良好:

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)

如果我是从根页面(即StaticPagesController)运行查询,查询发送到StaticPagesController而不是JobsController:

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.

推荐答案

我可以在下面看到一个多余的 form 标签您提供的表单摘要的顶部。使用简单表单,您只需要使用它提供的帮助程序即可:

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>

并将其重写为:

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

这篇关于搜索表单未路由到Rails 5.1中的适当控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆