Rails Ajax POST 请求上的 404 尽管已定义路由
Rails 404 on Ajax POST request despite route being defined
我是运行宁:
Rails 6
Ruby 2.5
Postgres 11.5
Mac OSX
我定义了以下路由:
Rails.application.routes.draw do
get 'welcome/index'
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
root 'welcome#index'
post 'search_controller/search'
end
我认为有以下代码:
<%= form_for :search, :url => url_for(:controller => 'search_controller', :action => 'search'), remote: true do |f| %>
<%= f.text_field :search_term %>
<%= f.submit %>
生成的表单代码为:
<form action="/search_controller/search" accept-charset="UTF-8" data-remote="true" method="post">
<input type="text" name="search[search_term]" id="search_search_term">
<input type="submit" name="commit" value="Save Search" data-disable-with="Save Search">
</form>
在我看来,这符合我的预期。我还尝试了 rails 6 种风格的形式,按照 :
<%= form_with(url: "/search_controller/search", method: "post") do %>
<%= label_tag(:q, "Search for:") %>
<%= text_field_tag(:term) %>
<%= submit_tag("Search") %>
这会导致同样的问题。
当我点击按钮时,我收到 HTTP 404 响应:
rails-ujs.js:215 POST http://localhost:40250/search_controller/search 404 (Not Found)
当我 运行 rails routes
我得到:
Prefix Verb URI Pattern Controller#Action
welcome_index GET /welcome/index(.:format) welcome#index
root GET / welcome#index
search_controller_search POST /search_controller/search(.:format) search_controller#search
rails_mandrill_inbound_emails POST /rails/action_mailbox/mandrill/inbound_emails(.:format) action_mailbox/ingresses/mandrill/inbound_emails#create
rails_postmark_inbound_emails POST /rails/action_mailbox/postmark/inbound_emails(.:format) action_mailbox/ingresses/postmark/inbound_emails#create
rails_relay_inbound_emails POST /rails/action_mailbox/relay/inbound_emails(.:format) action_mailbox/ingresses/relay/inbound_emails#create
rails_sendgrid_inbound_emails POST /rails/action_mailbox/sendgrid/inbound_emails(.:format) action_mailbox/ingresses/sendgrid/inbound_emails#create
rails_mailgun_inbound_emails POST /rails/action_mailbox/mailgun/inbound_emails/mime(.:format) action_mailbox/ingresses/mailgun/inbound_emails#create
rails_conductor_inbound_emails GET /rails/conductor/action_mailbox/inbound_emails(.:format) rails/conductor/action_mailbox/inbound_emails#index
POST /rails/conductor/action_mailbox/inbound_emails(.:format) rails/conductor/action_mailbox/inbound_emails#create
new_rails_conductor_inbound_email GET /rails/conductor/action_mailbox/inbound_emails/new(.:format) rails/conductor/action_mailbox/inbound_emails#new
edit_rails_conductor_inbound_email GET /rails/conductor/action_mailbox/inbound_emails/:id/edit(.:format) rails/conductor/action_mailbox/inbound_emails#edit
rails_conductor_inbound_email GET /rails/conductor/action_mailbox/inbound_emails/:id(.:format) rails/conductor/action_mailbox/inbound_emails#show
PATCH /rails/conductor/action_mailbox/inbound_emails/:id(.:format) rails/conductor/action_mailbox/inbound_emails#update
PUT /rails/conductor/action_mailbox/inbound_emails/:id(.:format) rails/conductor/action_mailbox/inbound_emails#update
DELETE /rails/conductor/action_mailbox/inbound_emails/:id(.:format) rails/conductor/action_mailbox/inbound_emails#destroy
rails_conductor_inbound_email_reroute POST /rails/conductor/action_mailbox/:inbound_email_id/reroute(.:format) rails/conductor/action_mailbox/reroutes#create
rails_service_blob GET /rails/active_storage/blobs/:signed_id/*filename(.:format) active_storage/blobs#show
rails_blob_representation GET /rails/active_storage/representations/:signed_blob_id/:variation_key/*filename(.:format) active_storage/representations#show
rails_disk_service GET /rails/active_storage/disk/:encoded_key/*filename(.:format) active_storage/disk#show
update_rails_disk_service PUT /rails/active_storage/disk/:encoded_token(.:format)
请注意,您可以在 search_controller_search 看到路线。
鉴于这些详细信息,为什么我会收到 404?
问题出在路由定义中。
解决方案是更改:
post 'search_controller/search'
至:
post 'search_controller/search', to:"search#search"
我是运行宁:
Rails 6 Ruby 2.5 Postgres 11.5 Mac OSX
我定义了以下路由:
Rails.application.routes.draw do
get 'welcome/index'
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
root 'welcome#index'
post 'search_controller/search'
end
我认为有以下代码:
<%= form_for :search, :url => url_for(:controller => 'search_controller', :action => 'search'), remote: true do |f| %>
<%= f.text_field :search_term %>
<%= f.submit %>
生成的表单代码为:
<form action="/search_controller/search" accept-charset="UTF-8" data-remote="true" method="post">
<input type="text" name="search[search_term]" id="search_search_term">
<input type="submit" name="commit" value="Save Search" data-disable-with="Save Search">
</form>
在我看来,这符合我的预期。我还尝试了 rails 6 种风格的形式,按照 :
<%= form_with(url: "/search_controller/search", method: "post") do %>
<%= label_tag(:q, "Search for:") %>
<%= text_field_tag(:term) %>
<%= submit_tag("Search") %>
这会导致同样的问题。
当我点击按钮时,我收到 HTTP 404 响应:
rails-ujs.js:215 POST http://localhost:40250/search_controller/search 404 (Not Found)
当我 运行 rails routes
我得到:
Prefix Verb URI Pattern Controller#Action
welcome_index GET /welcome/index(.:format) welcome#index
root GET / welcome#index
search_controller_search POST /search_controller/search(.:format) search_controller#search
rails_mandrill_inbound_emails POST /rails/action_mailbox/mandrill/inbound_emails(.:format) action_mailbox/ingresses/mandrill/inbound_emails#create
rails_postmark_inbound_emails POST /rails/action_mailbox/postmark/inbound_emails(.:format) action_mailbox/ingresses/postmark/inbound_emails#create
rails_relay_inbound_emails POST /rails/action_mailbox/relay/inbound_emails(.:format) action_mailbox/ingresses/relay/inbound_emails#create
rails_sendgrid_inbound_emails POST /rails/action_mailbox/sendgrid/inbound_emails(.:format) action_mailbox/ingresses/sendgrid/inbound_emails#create
rails_mailgun_inbound_emails POST /rails/action_mailbox/mailgun/inbound_emails/mime(.:format) action_mailbox/ingresses/mailgun/inbound_emails#create
rails_conductor_inbound_emails GET /rails/conductor/action_mailbox/inbound_emails(.:format) rails/conductor/action_mailbox/inbound_emails#index
POST /rails/conductor/action_mailbox/inbound_emails(.:format) rails/conductor/action_mailbox/inbound_emails#create
new_rails_conductor_inbound_email GET /rails/conductor/action_mailbox/inbound_emails/new(.:format) rails/conductor/action_mailbox/inbound_emails#new
edit_rails_conductor_inbound_email GET /rails/conductor/action_mailbox/inbound_emails/:id/edit(.:format) rails/conductor/action_mailbox/inbound_emails#edit
rails_conductor_inbound_email GET /rails/conductor/action_mailbox/inbound_emails/:id(.:format) rails/conductor/action_mailbox/inbound_emails#show
PATCH /rails/conductor/action_mailbox/inbound_emails/:id(.:format) rails/conductor/action_mailbox/inbound_emails#update
PUT /rails/conductor/action_mailbox/inbound_emails/:id(.:format) rails/conductor/action_mailbox/inbound_emails#update
DELETE /rails/conductor/action_mailbox/inbound_emails/:id(.:format) rails/conductor/action_mailbox/inbound_emails#destroy
rails_conductor_inbound_email_reroute POST /rails/conductor/action_mailbox/:inbound_email_id/reroute(.:format) rails/conductor/action_mailbox/reroutes#create
rails_service_blob GET /rails/active_storage/blobs/:signed_id/*filename(.:format) active_storage/blobs#show
rails_blob_representation GET /rails/active_storage/representations/:signed_blob_id/:variation_key/*filename(.:format) active_storage/representations#show
rails_disk_service GET /rails/active_storage/disk/:encoded_key/*filename(.:format) active_storage/disk#show
update_rails_disk_service PUT /rails/active_storage/disk/:encoded_token(.:format)
请注意,您可以在 search_controller_search 看到路线。
鉴于这些详细信息,为什么我会收到 404?
问题出在路由定义中。
解决方案是更改:
post 'search_controller/search'
至:
post 'search_controller/search', to:"search#search"