Rails 404 по запросу Ajax POST, несмотря на то, что маршрут определен - PullRequest
0 голосов
/ 29 сентября 2019

Я работаю:

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

У меня есть следующий код в моемview:

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

1 Ответ

1 голос
/ 30 сентября 2019

Проблема была в определении маршрутов.

Решение было изменить:

 post 'search_controller/search'

на:

post 'search_controller/search', to:"search#search"
...