导轨:自定义路线不起作用

时间:2018-07-22 16:22:49

标签: ruby-on-rails routes

我正在尝试在Rails应用中创建自定义路线。它应该是脚手架生成的新记录表单的变体。我正在尝试添加new_task(我的应用中的一种活动)。

但是,当我导航到链接时,出现以下错误

ActiveRecord::RecordNotFound in ActivitiesController#show
Couldn't find Activity with 'id'=new_task
# Use callbacks to share common setup or constraints between actions.

def set_activity
  @activity = Activity.find(params[:id])
end

def set_customer

我的路线文件

resources :customers do

resources :contacts, :assets, :activities
  get 'activities/new_task', to: 'activities#new', as: :new_task  
end

我的耙路输出

customer_activities  GET    /customers/:customer_id/activities(.:format)          activities#index
                      POST   /customers/:customer_id/activities(.:format)          activities#create
new_customer_activity GET    /customers/:customer_id/activities/new(.:format)      activities#new
edit_customer_activity GET    /customers/:customer_id/activities/:id/edit(.:format) activities#edit
    customer_activity GET    /customers/:customer_id/activities/:id(.:format)      activities#show
                      PATCH  /customers/:customer_id/activities/:id(.:format)      activities#update
                      PUT    /customers/:customer_id/activities/:id(.:format)      activities#update
                      DELETE /customers/:customer_id/activities/:id(.:format)      activities#destroy
    customer_new_task GET    /customers/:customer_id/activities/new_task(.:format) activities#new
            customers GET    /customers(.:format)                                  customers#index
                      POST   /customers(.:format)                                  customers#create

我试图使用现有控制器的New方法,但是它似乎想接收参数的输入。我在做什么错了?

1 个答案:

答案 0 :(得分:0)

您必须将路线定义为集合路线,而不是成员路线。

resources :customers do
  resources :contacts, :assets
  resources :activities do
    collection do
      get :new_task
    end
  end
end