1

I have a /articles page that I can access via the articles_path route helper.

Let's say I have 2 tabs on that page (e.g. something like this) that the user can click back and forth on, but it doesn't leave the page.

I have logic that allows the user to deep link to a specific tab, so either of the following url's are valid and will open the page on the specified tab directly.

  • /articles?tab=foo
  • /articles?tab=bar

Is it possible to define two new custom routes with the above urls that include the query parameter? I'd love to have a helper like articles_foo_tab_path and articles_bar_tab_path that incorporate those query parameters directly.

Thanks!

2 Answers 2

2

Create those helper methods:

module ArticlesHelper
  def articles_foo_tab_path(article)
    article_path(article, tab: 'foo')
  end

  def articles_foo_bar_path(article)
    article_path(article, tab: 'bar')
  end
end

And use them in your views:

<%= link_to @article.title, articles_foo_bar_path(@article) %>
Sign up to request clarification or add additional context in comments.

Comments

1

The helper method is one solution. Alternatively you can add a route which maps the tab param to the url e.g. articles/foo or articles/bar

get "articles(filter/:filter)", to: "articles#index", filter: /.*/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.