skip to Main Content

I’m taking an MOOC and the goal of this exercise is to add a new functionality to typo, where i can merge two articles together.

When I add the route to my new function merge to the routes.rb I’m losing the functionality to delete articles. I think something clashes here, but I have no idea what.

The original routes.rb:

%w{advanced cache categories comments content profiles feedback general pages
resources sidebar textfilters themes trackbacks users settings tags redirects seo post_types }.each do |i|
  match "/admin/#{i}", :to => "admin/#{i}#index", :format => false
  match "/admin/#{i}(/:action(/:id))", :to => "admin/#{i}", :action => nil, :id => nil, :format => false
end

This method in articles.rb creates the correct url for deleting

def delete_url
 blog.url_for(:controller => "/admin/content", :action =>"destroy",:id => id)
end

correct url:

http://example.com/admin/content/destroy/7

If i follow this link i can successfully delete an article.

However, if I add the following before that to my routes.rb:

namespace "admin" do
 resources :content do
   post :merge, on: :member, as: :merge
 end
end

The new merging functionality and forms are working fine, but the method delete_url now produces something like this:

http://example.com/admin/content/7

and if I follow a link created by this method i get:

Unknown action

The action 'show' could not be found for Admin::ContentController

Maybe I’m overwriting something? I can’t figure out what’s happening here and why this affects the delete action / route.

Thanks in advance!

EDIT: rake routes | grep content:

with the original routes.rb gives me:

admin_content        /admin/content                     {:controller=>"admin/content", :action=>"index"}
                     /admin/content(/:action(/:id))     {:action=>nil, :id=>nil, :controller=>"admin/content"}

whereas my modified routes.rb produces

merge_admin_content POST   /admin/content/:id/merge(.:format) {:action=>"merge", :controller=>"admin/content"}
admin_content_index GET    /admin/content(.:format)           {:action=>"index", :controller=>"admin/content"}
                   POST   /admin/content(.:format)           {:action=>"create", :controller=>"admin/content"}
 new_admin_content GET    /admin/content/new(.:format)       {:action=>"new", :controller=>"admin/content"}
edit_admin_content GET    /admin/content/:id/edit(.:format)  {:action=>"edit", :controller=>"admin/content"}
     admin_content GET    /admin/content/:id(.:format)       {:action=>"show", :controller=>"admin/content"}
                   PUT    /admin/content/:id(.:format)       {:action=>"update", :controller=>"admin/content"}
                   DELETE /admin/content/:id(.:format)       {:action=>"destroy", :controller=>"admin/content"}
                          /admin/content                     {:controller=>"admin/content", :action=>"index"}
                          /admin/content(/:action(/:id))     {:action=>nil, :id=>nil, :controller=>"admin/content"}

2

Answers


  1. Chosen as BEST ANSWER

    Okay, thanks to @guitarman i worked through my routes code and found out I can add the following except:

    namespace "admin" do
     resources :content, except: [:index, :show, :update, :destroy, :edit, :new, :create] do
       post :merge, on: :member, as: :merge
     end
    end
    

    after that, the rake routes just shows the additional merge route I wanted and my destroy action works fine again.


  2. Check rake routes command. I think there is a route /admin/content/:id which will be created by resources :content in the namespace "admin".
    Your request to http://example.com/admin/content/7 will be catched be the defined route but I gess you have no show action in the controller.

    Better:

    namespace "admin" do
      post "/content/:id/merge", to: "admin/content#merge", as: :merge
    end
    

    For more information about recources and the CRUD operations please see the rails routing guide.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search