Rails路由有时会混淆对象ID

时间:2016-07-28 14:14:59

标签: ruby-on-rails mongodb object methods

所以我有一个带有控制器的rails应用程序,它有很多方法。由于某些原因,应用程序无法正确解释对此控制器的调用。例如:

    A 404 error occurred on the Production server at 2016-07-28 02:55:23 UTC:
Message:

Problem: Document(s) not found for class Section with id(s) publish_preview. Summary: When calling Section.find ... [etc.]

Requested Params

    {"action"=>"update", "controller"=>"sections", "organization_id"=>"goannunciation", "bulletin_id"=>"20160731", "id"=>"publish_preview"}

Url:

bulletinbuilder.org/organizations/goannunciation/bulletins/20160731/sections/publish_preview

因此,sections控制器有一个“publish_preview”方法,该URL应该 - 并且通常会 - 适当地调用。但是,最近该应用程序没有正确解释URL;从堆栈:

"/var/www/bulletin_builder/releases/20160725175809/app/controllers/sections_controller.rb:34:in `update'"

表示应用程序正在调用“更新方法”,使用实际方法调用“publish_preview”作为节的id。这必须是某种路由错误...可能与标题有关?浏览器相关?

 resources :organizations do
  ...
  resources :bulletins do
   ...
   resources :sections do
    collection do
      get :manage
      get :first_section
      get :bulletin_creation
      get :included_sections
      get :custom_sections_max_message
      get :search
      get :publish_preview
      get :admin_diagnostics
    end
    member do
      post :remove
      post :removed_blocked_section
      get :delete_dynamic
      get :update_included
      get :update_added
      get :share
      get :unshare
      get :refresh
    end
  end

正如我在下面的评论中所提到的,相关的路线没有改变,只是偶尔会发生这个问题。

此外,由于某种原因,服务器正在发送一堆(17)错误通知电子邮件以发现相同的错误,所有这些都在几秒钟内完成。

2 个答案:

答案 0 :(得分:0)

这很可能与用于访问资源的HTTP方法有关。例如,

GET /organizations/../sections/publish_preview

将按预期点击publish_preview行动;然而

PUT/POST /organizations/../sections/publish_preview

update :id行动publish_preview作为resources :sections, constraints: { id: /\d+/} do ... end

如果您实际使用更新操作,则可以添加约束以仅路由数字ID

resources :sections, except: [:update]

如果您没有使用更新操作,只需删除更新路由

{{1}}

还有其他方法可以解决这个问题,但根据您的情况,上述两种方法可能最容易实现。

答案 1 :(得分:0)

所以这是一个非常奇怪的间歇性问题。它有点复杂,但显然归结为:

有一种方法可以调用一堆其他方法。其中一些方法被放在一个单独的线程中,因为它们需要一段时间才能运行。在调用其他方法的线程完成之前,封闭方法有时以“return_to:back”结束。

这显然对应用程序做了一些时髦的事情,包括搞乱运行线程中使用的参数,并导致一些非常奇怪的错误。通过使用render json:statement替换return_to:back来解决该问题。