gpt4 book ai didi

ruby-on-rails - 搜索表单未路由到 Rails 5.1 中的正确 Controller

转载 作者:行者123 更新时间:2023-11-29 13:17:13 25 4
gpt4 key购买 nike

我已经在这个问题上苦苦挣扎了几天:

我的导航栏中有一个搜索表单,它通过作业模型在我的用户表的 :address 列中运行搜索。这是我的 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

结束

Jobs 路由在那里两次,因为我需要为未登录的用户/访问者提供索引和 View 操作

我的部分 rails 路线显示: rake routes

我的问题是,请求在路由中丢失,有时会到达错误的 Controller 。如果我在 Jobs#index 页面上:http://localhost:3000/jobs并搜索像“南特”这样的城市。查询运行良好:

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:

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

无论我身在何处,如何确保始终将查询发送到正确的 Controller ?我在表格中尝试过的任何东西似乎都不起作用!谢谢你们。

最佳答案

我可以在您提供的表单片段的顶部看到一个多余的 form 标签。使用 Simple Form,您只需使用它提供的帮助程序:

<%= simple_form_for @user do |f| %>
<%= f.input :username %>
<%= f.input :password %>
<%= f.button :submit %>
<% 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>

并重写为:

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

关于ruby-on-rails - 搜索表单未路由到 Rails 5.1 中的正确 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47385188/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com