略過內容 略過搜尋

Action Dispatch Routing PolymorphicRoutes

多態 URL 輔助工具是智慧解析指定 Active Record 型別實例後命名路由呼叫的方法。它們旨在搭配 ActionController::Resources 使用。

當想要產生正確的 URL 或路徑以連結到 RESTful 資源,且不必瞭解問題中記錄的確切型別時,這些方法會相當有用。

巢狀資源和/或命名空間也獲得支援,範例說明如下

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

的結果

admin_article_comment_url(@article, @comment)

在架構中的用法

多態式的 URL 輔助程式會於 Rails 框架中各處使用

  • url_for,因此您可以將它與記錄作為引數搭配使用,例如,url_for(@article)

  • ActionView::Helpers::FormHelper 使用 polymorphic_path,因此您可以撰寫 form_with(model: @article),而無須為表單動作指定 :url 參數

  • redirect_to(事實上,redirect_to 會使用 url_for),因此您可以在控制器中撰寫 redirect_to(post)

  • ActionView::Helpers::AtomFeedHelper,因此您無須明確指定供應項目網址。

多態的前置輔助工具

除了 polymorphic_urlpolymorphic_path 方法之外,許多前置輔助工具可作為 options 中 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_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