跳至内容 跳至搜索

操作分发 Routing PolymorphicRoutes

多态 URL 帮助器是用于在给定活动记录模型实例时智能解析到命名路由调用的方法。它们要与 ActionController::Resources 结合使用。

当您希望生成到 RESTful 资源的正确 URL 或路径而无需了解相关记录的确切类型时,这些方法非常有用。

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

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

生成

admin_article_comment_url(@article, @comment)

框架内的用法

多态 URL 帮助器在整个 Rails 框架中的许多地方使用

  • url_for,因此您可以使用它将记录作为参数,例如 url_for(@article)

  • ActionView::Helpers::FormHelper 使用 polymorphic_path,因此您可以编写 form_for(@article) 而无需为表单操作指定 :url 参数;

  • redirect_to(实际上,它使用 url_for),因此您可以在控制器中编写 redirect_to(post)

  • ActionView::Helpers::AtomFeedHelper,因此您不必显式指定信息条目的 URL。

带前缀的多态帮助器

除了 polymorphic_urlpolymorphic_path 方法之外,还有一些带前缀的帮助器可用作选项中 action: "..." 的速记。它们是

  • edit_polymorphic_urledit_polymorphic_path

  • new_polymorphic_urlnew_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_for([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 126
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 103
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