Module: Gen::UrlHelpers

Defined in:
docs/api/stubs/generated_app.rb

Overview

The *_path and *_url helpers spin run gen writes into gen/routes.rb, one pair per named route in config/routes.rb. They are included into every controller and resolved by name in templates. These are the ones examples/blog’s routes generate:

Cybertrain::Routes.draw do
  root "articles#index"
  resources :articles do |articles|
    articles.resources :comments, only: [:create, :destroy]
  end
end

Arguments are positional, one per :param in the path: a record (its to_param, the id), an Integer or a String, percent-encoded as one segment. nil raises ArgumentError "missing route parameter". *_url prepends Cybertrain.url_root.

Keywords after them land in the trailing options Hash (spec 2.6): format: adds an extension, anchor: a fragment, and every other key becomes a query parameter, in the order given, escaped. A nil value leaves its key out, an Array gives key[]=v per element, a Hash raises ArgumentError. The same keywords work on a path helper in a template.

article_path(@article, page: 2)                 # => "/articles/3?page=2"
article_path(@article, format: :json)           # => "/articles/3.json"
articles_path(q: "a b", tag: ["x", "y"])        # => "/articles?q=a+b&tag%5B%5D=x&tag%5B%5D=y"
article_path(@article, page: 2, anchor: "c")    # => "/articles/3?page=2#c"

See Cybertrain::Routes.draw for the names each route gets.

Instance Method Summary collapse

Instance Method Details

#article_comment_path(article, comment, options = {}) ⇒ String

The nested member.

Parameters:

Returns:

  • (String) —

    "/articles/1/comments/2"



1293
# File 'docs/api/stubs/generated_app.rb', line 1293

def article_comment_path(article, comment, options = {}); end

#article_comments_path(article, options = {}) ⇒ String

The nested collection: one argument per level.

Parameters:

  • article (Cybertrain::Model, Integer, String)
  • options (Hash{Symbol => Object}) (defaults to: {}) —

    format:, anchor: and query keys

Returns:

  • (String) —

    "/articles/1/comments"



1285
# File 'docs/api/stubs/generated_app.rb', line 1285

def article_comments_path(article, options = {}); end

#article_path(article, options = {}) ⇒ String

Returns "/articles/1".

Examples:

redirect_to article_path(@article), status: :see_other
article_path(@article, format: :json) # => "/articles/3.json"

Parameters:

  • article (Cybertrain::Model, Integer, String)
  • options (Hash{Symbol => Object}) (defaults to: {}) —

    format:, anchor: and query keys

Returns:

  • (String) —

    "/articles/1"



1272
# File 'docs/api/stubs/generated_app.rb', line 1272

def article_path(article, options = {}); end

#articles_path(options = {}) ⇒ String

Returns "/articles".

Parameters:

  • options (Hash{Symbol => Object}) (defaults to: {}) —

    format:, anchor: and query keys

Returns:

  • (String) —

    "/articles"



1253
# File 'docs/api/stubs/generated_app.rb', line 1253

def articles_path(options = {}) = Cybertrain::Gen.with_options("/articles", options)

#articles_url(options = {}) ⇒ String

Returns Cybertrain.url_root + "/articles".

Parameters:

  • options (Hash{Symbol => Object}) (defaults to: {}) —

    as for articles_path

Returns:



1258
# File 'docs/api/stubs/generated_app.rb', line 1258

def articles_url(options = {}) = Cybertrain.url_root + articles_path(options)

#edit_article_path(article, options = {}) ⇒ String

Returns "/articles/1/edit".

Parameters:

  • article (Cybertrain::Model, Integer, String)
  • options (Hash{Symbol => Object}) (defaults to: {}) —

    format:, anchor: and query keys

Returns:

  • (String) —

    "/articles/1/edit"



1278
# File 'docs/api/stubs/generated_app.rb', line 1278

def edit_article_path(article, options = {}); end

#new_article_path(options = {}) ⇒ String

Returns "/articles/new".

Parameters:

  • options (Hash{Symbol => Object}) (defaults to: {}) —

    format:, anchor: and query keys

Returns:

  • (String) —

    "/articles/new"



1263
# File 'docs/api/stubs/generated_app.rb', line 1263

def new_article_path(options = {}) = Cybertrain::Gen.with_options("/articles/new", options)

#root_path(options = {}) ⇒ String

Returns "/".

Parameters:

  • options (Hash{Symbol => Object}) (defaults to: {}) —

    format:, anchor: and query keys

Returns:

  • (String) —

    "/"



1248
# File 'docs/api/stubs/generated_app.rb', line 1248

def root_path(options = {}) = Cybertrain::Gen.with_options("/", options)