实现嵌套表单时路由问题

时间:2015-11-22 20:32:39

标签: ruby ruby-on-rails-4

我正在使用Rails 4 Ruby 2构建CAD应用程序

我有一个Call模型和一个Update模型,我开始将Update嵌入到我的调用中。

我已经完成了视图和控制器嵌套等,但我似乎挂了我的Routes.rb。

我得到的错误是:

  

无法在资源范围之外使用集合

我的Routes.rb文件如下所示:

  resources :calls do 
      resources :updates, except: [:index], controller: 'calls/updates'
    end
      collection do
        get 'history'
      end
      member do
        patch :update_unit_on_scene
        patch :update_unit_clear
        patch :update_unit2_os
        patch :update_unit2_cl
        patch :update_unit3_os
        patch :update_unit3_cl
        patch :update_unit4_os
        patch :update_unit4_cl
      end

我对嵌套表单/视图等非常新,所以我认为这是我出错的地方。

我的完整路线文件:

Rails.application.routes.draw do

  devise_for :users, controllers: { registrations: 'registrations' }

  devise_scope :user do
    authenticated :user do
      root 'calls#index', as: :authenticated_root
    end

    unauthenticated do
      root 'devise/sessions#new', as: :unauthenticated_root
    end
  end

  resources :sites

  resources :calls do 
      resources :updates, except: [:index], controller: 'calls/updates'
    end
      collection do
        get 'history'
      end
      member do
        patch :update_unit_on_scene
        patch :update_unit_clear
        patch :update_unit2_os
        patch :update_unit2_cl
        patch :update_unit3_os
        patch :update_unit3_cl
        patch :update_unit4_os
        patch :update_unit4_cl
      end

end

1 个答案:

答案 0 :(得分:2)

您的路线文件似乎放错了end关键字。尝试编写路线文件,如下所示:

Rails.application.routes.draw do

  devise_for :users, controllers: { registrations: 'registrations' }

  devise_scope :user do
    authenticated :user do
      root 'calls#index', as: :authenticated_root
    end

    unauthenticated do
      root 'devise/sessions#new', as: :unauthenticated_root
    end
  end

  resources :sites

  resources :calls do 
    resources :updates, except: [:index], controller: 'calls/updates'

  # end keyword was placed here initially, which was closing off 
  # the resource scope for the routes being defined below,
  # causing the error you were seeing

    collection do
      get 'history'
    end

    member do
      patch :update_unit_on_scene
      patch :update_unit_clear
      patch :update_unit2_os
      patch :update_unit2_cl
      patch :update_unit3_os
      patch :update_unit3_cl
      patch :update_unit4_os
      patch :update_unit4_cl
    end

  # moving end keyword to this position ensures that the calls resource
  # properly encloses the collection and member routes

  end
end

希望它有所帮助!