【发布时间】:2019-09-29 18:16:04
【问题描述】:
我正在跑步:
导轨 6 红宝石 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?
【问题讨论】:
-
我会查看呈现的页面的源代码并验证 URL 是否符合您的预期。此外,请查看您提交表单时生成的服务器日志 - 它是否收到您认为应该的内容?
-
先按照rails 6方式写下你的路由代码和
form_for语法...参考https://edgeguides.rubyonrails.org/index.html -
@dinjas 实际上我什至没有从服务器收到错误,那里根本没有问题,这很奇怪。我运行我的服务器:
foreman start -f Procfile.dev和 Procfile.dev 有: web: bundle exec puma -C config/puma.rb webpacker: ./bin/webpack-dev-server -
@nileshkumar 我现在也尝试过 - 这没有帮助并且有同样的问题 - 你的建议没有帮助。
标签: ruby-on-rails ruby routes rubygems ruby-on-rails-6