首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >基于Ajax POST请求的Rails 404,尽管定义了路由

基于Ajax POST请求的Rails 404,尽管定义了路由
EN

Stack Overflow用户
提问于 2019-09-30 02:16:04
回答 1查看 188关注 0票数 0

我正在运行:

Rails 6 Ruby 2.5 Postgres 11.5 Mac OSX

我定义了以下路由:

代码语言:javascript
复制
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

我的视图中有以下代码:

代码语言:javascript
复制
 <%= form_for :search, :url => url_for(:controller => 'search_controller', :action => 'search'), remote: true do |f| %>
  <%= f.text_field :search_term %>
  <%= f.submit %>

生成的表单代码为:

代码语言:javascript
复制
<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样式的表单,如下所示:

代码语言:javascript
复制
<%= form_with(url: "/search_controller/search", method: "post") do %>
  <%= label_tag(:q, "Search for:") %>
  <%= text_field_tag(:term) %>
  <%= submit_tag("Search") %>

这也导致了同样的问题。

当我单击该按钮时,我得到一个HTTP 404响应:

代码语言:javascript
复制
rails-ujs.js:215 POST http://localhost:40250/search_controller/search 404 (Not Found)

当我运行rails routes时,我得到:

代码语言:javascript
复制
   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?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-09-30 07:37:53

问题出在路由定义中。

解决方案是改变:

代码语言:javascript
复制
 post 'search_controller/search'

至:

代码语言:javascript
复制
post 'search_controller/search', to:"search#search"
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58157950

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档