未启动用于创建操作的自定义 POST 路由 [英] Custom POST routes for create action not fired up

查看:33
本文介绍了未启动用于创建操作的自定义 POST 路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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! :)"

# 问题

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

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_for(@forum) do |f| %>
....
<% end %>

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

I am using standard form_for helper.

$ 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

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

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

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"}

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

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

puts "Thanks!"

推荐答案

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

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

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

或者简写版本:

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

这篇关于未启动用于创建操作的自定义 POST 路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆