双命名空间路由问题

发布于 2024-12-22 11:25:50 字数 2094 浏览 2 评论 0原文

我正在尝试遵循这个 示例。 我在控制器中创建了一个操作:

  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

并且在“config/routes.rb”文件中创建了一条路线:

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

我还尝试将路线移动到集合块之外的操作,如下所示:

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

但我收到此错误在这两种情况下:

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

我太绿了,无法弄清楚这一点。

更新:

啊是的,需要记住更频繁地检查rake paths。我确实看到了这一点:

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

所以我改变了我的观点:

      <%=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%>

但我仍然收到类似的错误消息:

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

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}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浮萍、无处依 2024-12-29 11:25:50

两个问题:

第一

您在 POST 请求期间没有传入 :workflow_id。如果你查看你的rake paths结果,你会发现这是必要的:

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

第二

当你像这样命名路由时,你就告诉它你也反映了这一点控制器内的命名空间也是如此。

因此

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

,这意味着您需要在控制器中执行此操作:

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

如果您不想像那样为控制器命名空间,那么您需要将路由语法切换为:

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

这仍然会为您提供相同的路由方案,但不会强制您像以前一样执行控制器模块前缀。请记住,如果您切换到作用域方法,您的路径名将会更改,因此运行rake paths来获取新的路径名。

更多信息:http://guides.rubyonrails.org/routing.html# controller-namespaces-and-routing


更新:

我认为你让这个变得比它需要的更复杂一些。您的 link_to 可以简化为:

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

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 %>
关于从前 2024-12-29 11:25:50

您拥有作为成员的 distribute_resume 操作,而不是集合操作。这是你的意图吗?您将其称为收集操作。

因此,要么将路由声明移至 collection do 部分(如果它应该是一个集合操作),要么在重定向中传入工作流 ID。

无论哪种方式,您都还必须重命名重定向路径,因为它实际上并不调用 distribute_resume 操作,而是调用索引操作。

您当前拥有:

redirect_to admin_distributions_workflows_path

并且需要将其重命名为(集合版本):

redirect_to admin_distributions_workflows_distribute_resume_path

或(成员版本):

redirect_to admin_distributions_workflows_distribute_resume_path(@some_workflow_or_distribution_object)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文