sarahbones
sarahbones

Reputation: 23

rails routes cannot redirect correctly

my data hierarchy is 'Case' has many 'Categories' has many 'Tasks'

I have followed document that Resources should never be nested more than 1 level deep. Therefore, I use shallow as below :

my routes.rb

resources :cases do
    resources :categories, :shallow => true do
      resources :tasks, :shallow => true
  end
end

rake routes

     category_tasks GET    /categories/:category_id/tasks(.:format)     tasks#index
                       POST   /categories/:category_id/tasks(.:format)     tasks#create
     new_category_task GET    /categories/:category_id/tasks/new(.:format) tasks#new
             edit_task GET    /tasks/:id/edit(.:format)                    tasks#edit
                  task GET    /tasks/:id(.:format)                         tasks#show
                       PATCH  /tasks/:id(.:format)                         tasks#update
                       PUT    /tasks/:id(.:format)                         tasks#update
                       DELETE /tasks/:id(.:format)                         tasks#destroy
       case_categories GET    /cases/:case_id/categories(.:format)         categories#index
                       POST   /cases/:case_id/categories(.:format)         categories#create
     new_case_category GET    /cases/:case_id/categories/new(.:format)     categories#new
         edit_category GET    /categories/:id/edit(.:format)               categories#edit
              category GET    /categories/:id(.:format)                    categories#show
                       PATCH  /categories/:id(.:format)                    categories#update
                       PUT    /categories/:id(.:format)                    categories#update
                       DELETE /categories/:id(.:format)                    categories#destroy
                 cases GET    /cases(.:format)                             cases#index
                       POST   /cases(.:format)                             cases#create
              new_case GET    /cases/new(.:format)                         cases#new
             edit_case GET    /cases/:id/edit(.:format)                    cases#edit
                  case GET    /cases/:id(.:format)                         cases#show
                       PATCH  /cases/:id(.:format)                         cases#update
                       PUT    /cases/:id(.:format)                         cases#update
                       DELETE /cases/:id(.:format)                         cases#destroy
                  root GET    /                                            cases#index

I have put category view into the case/show.html.erb to show the categories in this case.

the url of the cases#show is localhost:3000/cases/9

if I want to create a new category the link will be localhost:3000/cases/9/categories/new

it works well to redirect_to cases#show page with my categories#create action

def create
    @case = Case.find(params[:case_id])
    @category = Category.new(category_params)
    @category.case = @case
    @case.member = current_member

    if @category.save
      redirect_to case_path(@case)
    else
      render :new
    end
end

but when I try to edit the category and update it

the url change to localhost:3000/categories/2/edit that I don't have case_id anymore.

it could not redirect back to the case#show page localhost:3000/cases/9

I tried to write the similar code as my create action as below

def update
   @case = Case.find(params[:case_id])
   @category = Category.find(params[:id])
   @category.case = @case

   if @category.update(category_params)
      redirect_to case_path(@case)
   else
      render :edit
   end
end

but it cannot get the case_id and the error message as below

Couldn't find Case with 'id'=

how can I redirect to localhost:3000/cases/9 after edit and update 'category'?

Upvotes: 2

Views: 94

Answers (2)

Jayaprakash
Jayaprakash

Reputation: 1403

Since you don't want to change the case when you edit the category, try the below one.

def update
   @category = Category.find(params[:id])
   @case = @category.case

   if @category.update(category_params)
      redirect_to case_path(@case)
   else
      render :edit
   end
end

Upvotes: 0

w11th
w11th

Reputation: 26

The shallow: true changed the edit_category to a shallow route according to the document.There is no parent resource id in a shallow route.

You can use @case = @category.case instead. No need for the category_id param

Upvotes: 1

Related Questions