Search code examples
rubyurlopalrbvoltrb

How to access a page's URL in VoltRb


I'm trying to run some code on a controller in Volt, but only on certain pages. I've looked through the docs, but I'm not really sure how to get access to a given page's URL. Is there perhaps some hidden variable or something in the page model, like so?:

module Main
  class MyController < Volt::ModelController
    model :page

    def index
    end

    def template_page
      if page.url == "/foo/bar" # obviously, this doesn't actually work
        # run some code
      end
    end
  end
end

Solution

  • If you are using bindings in your URL, for example

    client '/examples/{{ category }}/{{ example }}', controller: 'examples', action: 'template'
    

    you can access those in the controller via the params collection:

    params._category
    params._example
    

    In other cases you URL should be static anyway.