【问题标题】:How to link to an action without a route helper如何在没有路由助手的情况下链接到操作
【发布时间】:2015-11-18 03:16:12
【问题描述】:

如何在没有路由助手的情况下链接到操作?

我有路线

get '/batches/:id/creation_wizard/add_funds' => 'batch::creation_wizard#add_funds'

如果我在 Rails 控制台中这样做

include Rails.application.routes.url_helpers
default_url_options[:host] = "localhost"
url_for(controller: 'batch::creation_wizard', action: 'add_funds', id: 1)

我收到"http://localhost/batches/1/creation_wizard/add_funds"

但如果我有

class Batch::CreationWizardController < ApplicationController
  def my_method
    redirect_to controller: 'batch/creation_wizard', action: 'add_funds', id: 1
  end
end

我明白了

No route matches {:controller=>"batch/batch::creation_wizard", :action=>"add_funds", :id=>1}

如果我尝试

redirect_to controller: 'creation_wizard', action: 'add_funds', id: 1

我明白了

No route matches {:controller=>"batch/creation_wizard", :action=>"add_funds", :id=>1}

如果我尝试

redirect_to action: 'add_funds', id: 1

我明白了

No route matches {:action=>"add_funds", :id=>1, :controller=>"batch/creation_wizard"}

我尝试阅读 Rails 指南“Rails Routing from the Outside In”和“Getting Started with Rails”,但没有发现任何帮助。

我可以将路由更改为

get '/batches/:id/creation_wizard/add_funds' => 'batch::creation_wizard#add_funds', as: :creation_wizard_add_funds

并依赖于路由助手,但这感觉很老套。

我正在使用 Rails 3.2.22。

我做错了什么?

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 rails-routing


    【解决方案1】:

    路线需要改成

    get '/batches/:id/creation_wizard/add_funds' => 'batch/creation_wizard#add_funds'
    

    我不明白为什么如果路由错误,我首先能够查看带有http://localhost:3000/batches/521/creation_wizard/add_funds 的页面。

    Rails 4.x 版指南mentions

    对于命名空间控制器,您可以使用目录表示法。为了 示例:

    资源:user_permissions,控制器:'admin/user_permissions' 这个 将路由到 Admin::UserPermissions 控制器。

    仅支持目录表示法。指定控制器 使用 Ruby 常量表示法(例如控制器:'Admin::UserPermissions') 可能会导致路由问题并导致警告。

    但不幸的是,Rails 指南的 3.2.x 版本中的 equivalent section 似乎没有提到这一点。

    【讨论】:

      【解决方案2】:

      这将解决它:

      class Batch::CreationWizardController < ApplicationController
        def my_method
          redirect_to controller: 'batch::creation_wizard', action: 'add_funds', id: 1
        end
      end
      

      您遇到的问题是 namespacednested 资源之间的混淆。

      Namespacing 旨在为您提供 模块 的功能(IE 将一定级别的功能与特定类型的资源相关联):

      #config/routes.rb
      namespace :batch do 
         resources :creation_wizard do
            get :add_funds, on: :member
         end
      end
      

      这将创建以下路线:

      {action: "show", controller:"batch::creation_wizard", id: "1"}
      

      命名空间基本上是为您提供一个文件夹来放置控制器。它最常用于admin 功能,允许您使用以下功能:

      #config/routes.rb
      namespace :admin do
         root "application#index"
      end
      
      #app/controllers/admin/application_controller.rb
      class Admin::ApplicationController < ActionController::Base
         ...
      end
      

      这是你目前拥有的。

      --

      如果您想使用 嵌套 路由(IE 使用“顶级”资源来影响“低级”资源),您必须将路由更改为以下内容:

      #config/routes.rb
      resources :batches do
         resources :creation_wizard do
            get :add_funds, on: :member
         end
      end
      

      这将提供以下路线:

      {controller: "creation_wizard", action: "add_funds", batch_id: params[:batch_id]}
      

      嵌套资源允许您定义“顶级”信息(在本例中为batch_id,然后将向下传递到被调用的控制器。路由看起来像url.com/batches/:batch_id/creation_wizard/add_funds

      【讨论】:

      • 为什么redirect_to action: 'add_funds', id: 1 在我给出的示例中不起作用?
      猜你喜欢
      • 2012-10-05
      • 1970-01-01
      • 2013-01-02
      • 1970-01-01
      • 2016-08-11
      • 2021-09-18
      • 1970-01-01
      • 2013-05-23
      • 2016-10-16
      相关资源
      最近更新 更多