Ramy
Ramy

Reputation: 21261

Double namespaced routing issues

I'm trying to follow this example. I've created an action in my controller:

  def distribute_resume
    Rails.logger.info(distribution_id.to_s)
    PartnerNotifier.distribute_resume(distribution_id)
    flash[:notice] = "Successfully distributed resume"
    redirect_to admin_distributions_workflows_path
  end

and I created a route in my `config/routes.rb' file:

  namespace :admin do
    namespace :distributions do
      resources :workflows do
        collection do
          post :edit_multiple
          put :update_multiple
          post :distribute_resume
        end
      end
    end
  end

I also tried moving the route to the action outside of the collection block like this:

  namespace :admin do
    namespace :distributions do
      resources :workflows do
        post :distribute_resume
        collection do
          post :edit_multiple
          put :update_multiple
        end
      end
    end
  end 

But i'm getting this error in both cases:

No route matches {:controller=>"admin/distributions/workflows_controller", :distribution_id=>123, :action=>"distribute_resume", :method=>:post}

I'm too green to figure this out.

update:

ah yes, need to remember to check rake routes more often. I do see this:

admin_distributions_workflow_distribute_resume POST   /admin/distributions/workflows/:workflow_id/distribute_resume(.:format) {:action=>"distribute_resume", :controller=>"admin/distributions/workflows"}

so I changed my view:

      <%=link_to "Send this resume to #{distribution.matching_profile.partner.email}",
            :controller => "workflows", <-- instead of "workflows_controller"
            :action => "distribute_resume",
            :distribution_id => distribution.id,
            :method => :post%>

but i'm still getting a similar error message:

No route matches {:controller=>"admin/distributions/workflows", :distribution_id=>121, :action=>"distribute_resume", :method=>:post}

Upvotes: 2

Views: 986

Answers (2)

iwasrobbed
iwasrobbed

Reputation: 46713

Two issues:

First

You are not passing in :workflow_id during your POST request. If you look at your rake routes results, you'll see it is necessary:

/admin/distributions/workflows/:workflow_id/distribute_resume(.:format)

Second

When you namespace the routes like that, you are telling it that you have also reflected that namespacing within the Controller as well.

So

namespace :admin do
  namespace :distributions do
    resources :workflows do
    end
  end
end

Means that you would need to do this in your controller:

class Admin::Distributions::WorkflowsController < ApplicationController
  # controller code goes here
end

If you'd rather not namespace your controllers like that, then you need to switch up your routing syntax to instead be:

scope "/admin" do
  scope "/distributions" do
    resources :workflows do
    end
  end
end

which will still give you the same routing scheme but will not force you to do the controller module prefixes like before. Keep in mind if you switch to the scoped method, your path names will change so run rake routes to get the new ones.

More info: http://guides.rubyonrails.org/routing.html#controller-namespaces-and-routing


Update:

I think you're making this a little more complicated then it needs to be. Your link_to can be simplified to this:

<% =link_to "Send this resume to #{distribution.matching_profile.partner.email}",
            admin_distributions_workflow_distribute_resume_path(distribution.id),
            :remote => true,
            :method => :post %>

Upvotes: 1

jefflunt
jefflunt

Reputation: 33954

You've got your distribute_resume action as a member, not a collection, action. Is this what you intended? You're calling it as if it was a collection action.

So, either move your route declaration into the collection do portion (if it's supposed to be a collection action), or pass in a workflow ID in your redirect.

Either way you're also going to have to rename your redirect path, because it doesn't actually call the distribute_resume action, it calls the index action.

You currently have:

redirect_to admin_distributions_workflows_path

And this will need to be renamed to something along the lines of either (collection version):

redirect_to admin_distributions_workflows_distribute_resume_path

or (member version):

redirect_to admin_distributions_workflows_distribute_resume_path(@some_workflow_or_distribution_object)

Upvotes: 1

Related Questions