跳至内容 跳至搜索

Action Dispatch Routing PolymorphicRoutes

多态 URL 助手(Polymorphic URL helpers)是在给定 Active Record 模型实例时,能智能地解析到命名路由调用的方法。它们应与 ActionController::Resources 结合使用。

当您想生成到 RESTful 资源的正确 URL 或路径,而无需知道所涉及记录的确切类型时,这些方法非常有用。

嵌套资源和/或命名空间也受到支持,如示例所示

polymorphic_url([:admin, @article, @comment])

结果为

admin_article_comment_url(@article, @comment)

在框架中的用法

多态 URL 助手在 Rails 框架的多个地方被使用

带前缀的多态助手

除了 polymorphic_urlpolymorphic_path 方法外,还有许多带前缀的助手可用,作为 action: "..." 在选项中的简写。这些是:

  • edit_polymorphic_url, edit_polymorphic_path

  • new_polymorphic_url, new_polymorphic_path

示例用法

edit_polymorphic_path(@post)           # => "/posts/1/edit"
polymorphic_path(@post, format: :pdf)  # => "/posts/1.pdf"

与挂载引擎的用法

如果您正在使用挂载的引擎,并且需要使用指向引擎路由的 polymorphic_url,请将引擎的路由代理作为该方法的第一个参数传入。例如:

polymorphic_url([blog, @post])  # calls blog.post_path(@post)
form_with(model: [blog, @post]) # => "/blog/posts/1"
方法
P

实例公共方法

polymorphic_path(record_or_hash_or_array, options = {})

返回给定记录的 URL 的路径部分。

# File actionpack/lib/action_dispatch/routing/polymorphic_routes.rb, line 133
def polymorphic_path(record_or_hash_or_array, options = {})
  if Hash === record_or_hash_or_array
    options = record_or_hash_or_array.merge(options)
    record  = options.delete :id
    return polymorphic_path record, options
  end

  if mapping = polymorphic_mapping(record_or_hash_or_array)
    return mapping.call(self, [record_or_hash_or_array, options], true)
  end

  opts   = options.dup
  action = opts.delete :action
  type   = :path

  HelperMethodBuilder.polymorphic_method self,
                                         record_or_hash_or_array,
                                         action,
                                         type,
                                         opts
end

polymorphic_url(record_or_hash_or_array, options = {})

构造一个对给定记录的命名 RESTful 路由的调用,并返回生成的 URL 字符串。例如:

# calls post_url(post)
polymorphic_url(post) # => "http://example.com/posts/1"
polymorphic_url([blog, post]) # => "http://example.com/blogs/1/posts/1"
polymorphic_url([:admin, blog, post]) # => "http://example.com/admin/blogs/1/posts/1"
polymorphic_url([user, :blog, post]) # => "http://example.com/users/1/blog/posts/1"
polymorphic_url(Comment) # => "http://example.com/comments"

选项

  • :action - 指定命名路由的动作前缀::new:edit。默认为无前缀。

  • :routing_type - 允许的值是 :path:url。默认为 :url

还包括 url_for 的所有选项。这些包括 :anchor:trailing_slash 等。下面的示例演示了用法:

polymorphic_url([blog, post], anchor: 'my_anchor')
  # => "http://example.com/blogs/1/posts/1#my_anchor"
polymorphic_url([blog, post], anchor: 'my_anchor', script_name: "/my_app")
  # => "http://example.com/my_app/blogs/1/posts/1#my_anchor"

有关所有这些选项,请参阅 url_for 的文档。

功能

# an Article record
polymorphic_url(record)  # same as article_url(record)

# a Comment record
polymorphic_url(record)  # same as comment_url(record)

# it recognizes new records and maps to the collection
record = Comment.new
polymorphic_url(record)  # same as comments_url()

# the class of a record will also map to the collection
polymorphic_url(Comment) # same as comments_url()
# File actionpack/lib/action_dispatch/routing/polymorphic_routes.rb, line 110
def polymorphic_url(record_or_hash_or_array, options = {})
  if Hash === record_or_hash_or_array
    options = record_or_hash_or_array.merge(options)
    record  = options.delete :id
    return polymorphic_url record, options
  end

  if mapping = polymorphic_mapping(record_or_hash_or_array)
    return mapping.call(self, [record_or_hash_or_array, options], false)
  end

  opts   = options.dup
  action = opts.delete :action
  type   = opts.delete(:routing_type) || :url

  HelperMethodBuilder.polymorphic_method self,
                                         record_or_hash_or_array,
                                         action,
                                         type,
                                         opts
end