【问题标题】:Mysterious AbstractController::ActionNotFound (Route is there)神秘的 AbstractController::ActionNotFound (路线在那里)
【发布时间】:2012-07-17 18:35:28
【问题描述】:

(轨道 3.0.7)

我的routes.rb 有这个:

namespace :admin do
  namespace :campus_hub do
    resources :billing_subscriptions, {
      :except => [:destroy, :new, :create]
    } do
      member do
        post :add_addon
      end
    end
  end
end

rake routes 显示这条路线:

add_addon_admin_campus_hub_billing_subscription POST   /admin/campus_hub/billing_subscriptions/:id/add_addon(.:format)                            {:action=>"add_addon", :controller=>"admin/campus_hub/billing_subscriptions"}

我的控制器(Admin::CampusHub::BillingSubscriptionsController)有方法add_addon

我在日志中做了一个如下所示的 POST:

Started POST "/admin/campus_hub/billing_subscriptions/50059f5be628f83b13000012/add_addon" for 33.33.33.1 at Tue Jul 17 20:21:17 +0200 2012

我得到这个错误:

AbstractController::ActionNotFound (The action '50059f5be628f83b13000012' could not be found for Admin::CampusHub::BillingSubscriptionsController)

我完全感到困惑。我发出的 POST 请求与路线完全匹配。为什么它认为ID是动作?希望我只是遗漏了一些明显的东西!

【问题讨论】:

  • 是的,编辑/更新路线工作正常。
  • 解决方法是将路由的动词定义为 GET 而不是 POST/DELETE。然后就可以了。
  • 你能把你的代码放在答案中吗?
  • 您将post :add_addon 更改为get :add_addon。这不是一个答案。这是一种解决方法。我还在寻找答案。
  • 只是出于好奇,您可以将其更改为 post :add_on 并且不使用成员块吗?您仍然可以访问该参数,只是前缀不同。

标签: ruby-on-rails ruby-on-rails-3 mongoid


【解决方案1】:

我从 rails 版本猜测您遇到了与此类似的问题:Routing error with Rails 3 with members 这是 rails 3 中的一个错误,请注意评论:Routing error with Rails 3 with members

你需要更换:

  member do
    post :add_addon
  end

这样的:

match "add_addon" => "billing_subscriptions#add_addon", :as => :add_addon, :via => :post

你会得到一个稍微交换的路径,像这样:admin_campus_hub_billing_subscription_add_addon_path,但它应该可以在 rails 3 和 4 中工作。

加起来就是这样的:

namespace :admin do
  namespace :campus_hub do
    resources :billing_subscriptions, {
      :except => [:destroy, :new, :create]
    } do
      match "add_addon" => "billing_subscriptions#add_addon", :as => :add_addon, :via => :post
    end
  end
end

请注意,完整的 rake 路由如下所示:

admin_campus_hub_billing_subscription_add_addon POST  /admin/campus_hub/billing_subscriptions/:billing_subscription_id/add_addon(.:format) admin/campus_hub/billing_subscriptions#add_addon

【讨论】:

    猜你喜欢
    • 2014-08-19
    • 1970-01-01
    • 2015-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多