0
votes

I am trying to create a custom route in my rails app. It should be a variant of the New Record Form generated by a scaffold. I am trying to add new_task(a type of activity in my app).

However, when I navigate to the link I get the following error

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

My Route File

resources :customers do

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

My Rake Routes Output

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

I was trying to use the existing controller's New method, however it seems to want to receive an input of parameter. What I am doing wrong?

1

1 Answers

0
votes

You have to define your route as collection route not a member route.

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