实例公共方法
direct(name, options = {}, &block) 链接
定义将添加到应用程序路由的自定义 URL 帮助方法。这允许您覆盖和/或替换路由帮助方法的默认行为,例如:
direct :homepage do "https://rubyonrails.cn" end direct :commentable do |model| [ model, anchor: model.dom_id ] end direct :main do { controller: "pages", action: "index", subdomain: "www" } end
传递给 direct 的块的返回值必须是 url_for 的一组有效参数,它将实际构建 URL 字符串。这可以是以下之一:
-
字符串,将其视为生成的 URL
-
哈希,例如:
{ controller: "pages", action: "index" } -
数组,将传递给
polymorphic_url -
Active Model 实例
-
Active Model 类
注意:可以在块中调用其他 URL 帮助方法,但要小心不要再次调用自定义 URL 帮助方法,否则会导致堆栈溢出错误。
您还可以为您的 URL 帮助方法定义指定默认选项,例如:
direct :browse, page: 1, size: 10 do |options| [ :products, options.merge(params.permit(:page, :size).to_h.symbolize_keys) ] end
在此实例中,params 对象来自执行块的上下文,例如,在控制器操作或视图中生成 URL。如果块在没有 params 对象的地方执行,例如:
Rails.application.routes.url_helpers.browse_path
那么它将引发一个 NameError。因此,在定义自定义 URL 帮助方法时,需要注意使用它的上下文。
注意:direct 方法不能在 namespace 或 scope 等作用域块中使用,如果检测到它在其中,将引发错误。
来源: 显示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/routing/mapper.rb, line 2374 def direct(name, options = {}, &block) unless @scope.root? raise RuntimeError, "The direct method can't be used inside a routes scope block" end @set.add_url_helper(name, options, &block) end
resolve(*args, &block) 链接
定义模型到 URL 的自定义多态映射。这会改变 polymorphic_url 的行为,进而改变传递模型实例时 link_to、form_with 和 form_for 的行为,例如:
resource :basket resolve "Basket" do [:basket] end
当 Basket 实例传递给 link_to、form_with 或 form_for 时,这将生成 “/basket”,而不是标准的 “/baskets/:id”。
注意:此自定义行为仅适用于传递单个模型实例的简单多态 URL,而不适用于更复杂的表单,例如:
# config/routes.rb resource :profile namespace :admin do resources :users end resolve("User") { [:profile] } # app/views/application/_menu.html.erb link_to "Profile", @current_user link_to "Profile", [:admin, @current_user]
第一个 link_to 将生成 “/profile”,而第二个将生成标准的 “/admin/users/1” 多态 URL。
您可以将选项传递给多态映射 - 块的参数数量需要为两个,因为实例作为第一个参数传递,例如:
resolve "Basket", anchor: "items" do |basket, options| [:basket, options] end
这将生成 URL “/basket#items”,因为当传递给 polymorphic_url 的数组中的最后一项是哈希时,它被视为正在调用的 URL 帮助方法的选项。
注意:resolve 方法不能在 namespace 或 scope 等作用域块中使用,如果检测到它在其中,将引发错误。
来源: 显示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/routing/mapper.rb, line 2426 def resolve(*args, &block) unless @scope.root? raise RuntimeError, "The resolve method can't be used inside a routes scope block" end options = args.extract_options! args = args.flatten(1) args.each do |klass| @set.add_polymorphic_mapping(klass, options, &block) end end