0

total newbie here ^^ So I have a bunch of static pages in my footer that I would like to create a route for. Right now, I am using get to create the routes as such:

Rails.application.routes.draw do
  root 'static_pages#home_page'
  get 'static_pages/secret'

  get 'static_pages/stripe_button'
  get 'about_us', to: 'static_pages#about_us'
  get 'rules', to: 'static_pages#rules'
  get 'faq', to: 'static_pages#faq'
  get 'community', to: 'static_pages#community'
  get 'terms', to: 'static_pages#terms'
  get 'privacy', to: 'static_pages#privacy'
end

Ideally, I am trying to condense all of that into a resources like so:

resources :static_pages, only: [:index] do
  get :terms, :community, :privacy, :rules, :faq, :about_us, :stripe_button, :secret
end

Here is the footer li for the pages:

<div class="col">
  <ul>
    <li><%= link_to "About Us", 'about_us', class: 'nav-link d-inline-block' %></li>
    <li><%= link_to "Rules", 'rules', class: 'nav-link d-inline-block' %></li>
    <li><%= link_to "FAQ", 'faq', class: 'nav-link d-inline-block' %></li>
  </ul>
</div>
<div class="col">
  <ul>
    <li><%= link_to "Community", 'community', class: 'nav-link d-inline-block' %></li>
    <li><%= link_to "Terms", 'terms', class: 'nav-link d-inline-block' %></li>
    <li><%= link_to "Privacy", 'privacy', class: 'nav-link d-inline-block' %></li>
  </ul>
</div>

How do I get rid of all the gets and condense all of it into one resources?

ruby 2.7.1 rails 6.1.3

Thx a lot :)

Balou
  • 21
  • 7

2 Answers2

1

First of all, resources is a concept that revoles around an actual resource, or record if you will. Your static routes don't really follow that, so you the way you did it is actually the right way to do it (see also here: https://guides.rubyonrails.org/routing.html#non-resourceful-routes).

If your sole purpose is refactoring your routes file, I wouldn't worry too much, in fact I would leave it as is because I think it's more important to follow the concepts.

If you still want to go ahead and you don't care too much what the actual urls look like you can do it like so:

resources :static_pages, only: [:index] do
    collection do
      get :terms, :community, :privacy, :rules, :faq, :about_us, :stripe_button, :secret
    end
  end

Urls will look like this then /static_pages/terms.

The collection makes sure there is no :id needed in the routes. You can always double check in the terminal with rails routes - also in order to find the prefix for the link_to helper (will be terms_static_pages_path).

Clara
  • 2,677
  • 4
  • 16
  • 31
0

You may find ideas there Rails routing: resources with only custom actions

This could be your solution:

scope '/', controller: :static_pages do
    get :terms, :community, :privacy, :rules, :faq, :about_us, :stripe_button, :secret
end

If you don't want to manually type all actions, you can use the class method .action_methods and exclude from it all actions for whom you need a specific route:

scope '/', controller: :static_pages do
  (StaticPagesController.action_methods.to_a - ["home_page", "secret"])
    .each do |action_method|
      get action_method
    end
end