Method: Flame::Dispatcher#path_to

Defined in:
lib/flame/dispatcher.rb

#path_to(ctrl, action = :index, args = {}) ⇒ String

Build a path to the given controller and action, with any expected params

Examples:

Path for ‘show(id)` method of `ArticlesController` with `id: 2`

path_to ArticlesController, :show, id: 2 # => "/articles/show/2"

Path for ‘new` method of `ArticlesController` with params

path_to ArticlesController, :new, params: { author_id: 1 }
# => "/articles/new?author_id=1"

Parameters:

  • ctrl (Flame::Controller)

    class of controller

  • action (Symbol) (defaults to: :index)

    method of controller

  • args (Hash) (defaults to: {})

    parameters for method of controller

Returns:

  • (String)

    path for requested method, controller and parameters

Raises:



97
98
99
100
101
102
103
104
105
# File 'lib/flame/dispatcher.rb', line 97

def path_to(ctrl, action = :index, args = {})
  route = @app.class.router.find_route(controller: ctrl, action: action)
  raise Errors::RouteNotFoundError.new(ctrl, action) unless route
  query = Rack::Utils.build_nested_query args.delete(:params)
  query = nil if query&.empty?
  path = route.path.assign_arguments(args)
  path = '/' if path.empty?
  URI::Generic.build(path: path, query: query).to_s
end