Ruby-on-rails 未启动用于创建操作的自定义 POST 路由

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11297623/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 03:37:26  来源:igfitidea点击:

Custom POST routes for create action not fired up

ruby-on-railsruby-on-rails-3

提问by roychri

# Explaining the context

# 解释上下文

puts "I am learning Rails, building a simple forum application."
puts "I am pretty satisfied to where I got so far but routes... "
puts "...still figuring them out."
puts "Been 2 days trying all sorts of things."
puts "This is where I am now, and something is not working as expected."
puts "Any help/pointers would be appreciated! :)"

# The Problem

# 问题

puts "I want my forum's create path to be '/helpcenter' and not '/helpcenter/cat'."
puts "When I access the form to create a new forum and I hit submit, "
puts "the form post to '/helpcenter' correctly (firebuged)"
puts "but I get the index, not the create!"
puts "I even put debugger in my create action but it is not being called."

# config/routes.rb

# 配置/routes.rb

scope "/helpcenter" do
  resources :cat, :controller => "forums", :as => :forums do
    resources :topics , :controller => "forum_topics", :as => :topics
    resources :posts, :controller => "forum_posts", :as => :posts
  end
end

match "/helpcenter" => "forums#index", :as => :forums
match "/helpcenter" => "forums#create", :via => :post, :as => :create_forum

I hope this problem is in the way I created the route. I tried many different things.

我希望这个问题是我创建路线的方式。我尝试了很多不同的东西。

# _form.html.erb

# _form.html.erb

<%= form_for(@forum) do |f| %>
....
<% end %>

I am using standard form_for helper.

我正在使用标准的 form_for 助手。

# Rake Routes for Forums

# 论坛的 Rake 路线

$ CONTROLLER=forums rake routes
delete_forum GET    /helpcenter/cat/:id/delete(.:format) forums#delete
      forums GET    /helpcenter/cat(.:format)            forums#index
             POST   /helpcenter/cat(.:format)            forums#create
   new_forum GET    /helpcenter/cat/new(.:format)        forums#new
  edit_forum GET    /helpcenter/cat/:id/edit(.:format)   forums#edit
       forum GET    /helpcenter/cat/:id(.:format)        forums#show
             PUT    /helpcenter/cat/:id(.:format)        forums#update
             DELETE /helpcenter/cat/:id(.:format)        forums#destroy
      forums        /helpcenter(.:format)                forums#index
create_forum POST   /helpcenter(.:format)                forums#create

We clearly see a route for POST /helpcenter which is binded to the create action of the forums controller.

我们清楚地看到 POST /helpcenter 的路由,它绑定到论坛控制器的创建操作。

# Logs

# 日志

Started POST "/helpcenter" for 127.0.0.1 at 2012-07-02 12:25:00 -0400
Processing by ForumsController#index as HTML
  Parameters: {"utf8"=>"?", "authenticity_token"=>"d5iVKCh234234=", "forum"=>{"name"=>"", "description"=>""}, "commit"=>"Save Changes"}

The logs clearly shows I am doing a POST on /helpcenter but that it fires up the index action instead of the create action!

日志清楚地显示我正在 /helpcenter 上执行 POST 但它启动了索引操作而不是创建操作!

# What am I doing wrong?

# 我究竟做错了什么?

puts "Thanks!"

回答by Stefan

I think the request matches your first forumsroute since you didn't specify a HTTP method. This should work:

我认为该请求与您的第一forums条路线匹配,因为您没有指定 HTTP 方法。这应该有效:

match "/helpcenter" => "forums#index", :via => :get, :as => :forums
match "/helpcenter" => "forums#create", :via => :post, :as => :create_forum

Or the shorthand version:

或者简写版本:

get "/helpcenter" => "forums#index", :as => :forums
post "/helpcenter" => "forums#create", :as => :create_forum

回答by railsdog

First glance shows that a POST against /helpcenter passes the rule for the forums#index match, which is encountered first, so that's what you get

乍一看,针对 /helpcenter 的 POST 通过了 forums#index 匹配的规则,这是首先遇到的,所以这就是你得到的

match "/helpcenter" => "forums#index", :as => :forums
match "/helpcenter" => "forums#create", :via => :post, :as => :create_forum

What about:

关于什么:

match "/helpcenter" => "forums#index", :via => :get, :as => :forums
match "/helpcenter" => "forums#create", :via => :post, :as => :create_forum