patternrubyrailsMinor
Rails controller action methods corresponding to static web pages
Viewed 0 times
actionmethodsrailscontrollerwebpagescorrespondingstatic
Problem
One of my rails controllers is horribly overcrowded with a bunch of methods that link to static web pages.
Controller
For each of the above actions I have seperate view files and routes defined for them.
View Files
Routes
It is quite embarrassing to have code that horrible. Is there any way I can optimize that?
Controller
def adventure
end
def cooking
end
def dancing
end
def programming
end
def reading
end
def running
end
def sports
end
def writing
endFor each of the above actions I have seperate view files and routes defined for them.
View Files
adventure.html.erb
cooking.html.erb
dancing.html.erb
programming.html.erb
reading.html.erb
running.html.erb
sports.html.erb
writing.html.erbRoutes
match '/adventure', to: 'pages#adventure', via: 'get'
match '/cooking', to: 'pages#fighting', via: 'get'
match '/dancing', to: 'pages#first_person_shooter', via: 'get'
match '/programming', to: 'pages#programming', via: 'get'
match '/reading', to: 'pages#reading', via: 'get'
match '/running', to: 'pages#running', via: 'get'
match '/sports', to: 'pages#sports', via: 'get'
match '/writing', to: 'pages#writing', via: 'get'It is quite embarrassing to have code that horrible. Is there any way I can optimize that?
Solution
You don't need to define empty actions in your Controller.
Your code should work even if you delete all the empty actions, leaving you only with the views and routes:
page_controller.rb:
views:
routes:
or, if you really want things super DRY:
Your code should work even if you delete all the empty actions, leaving you only with the views and routes:
page_controller.rb:
class PageController < ApplicationController
endviews:
adventure.html.erb
cooking.html.erb
dancing.html.erb
programming.html.erb
reading.html.erb
running.html.erb
sports.html.erb
writing.html.erbroutes:
match '/adventure', to: 'pages#adventure', via: 'get'
match '/cooking', to: 'pages#cooking', via: 'get'
match '/dancing', to: 'pages#dancing', via: 'get'
match '/programming', to: 'pages#programming', via: 'get'
match '/reading', to: 'pages#reading', via: 'get'
match '/running', to: 'pages#running', via: 'get'
match '/sports', to: 'pages#sports', via: 'get'
match '/writing', to: 'pages#writing', via: 'get'or, if you really want things super DRY:
%w(adventure cooking dancing programming reading running sports writing).each do |page|
match "/#{page}", to: "pages##{page}", via: 'get'
endCode Snippets
class PageController < ApplicationController
endadventure.html.erb
cooking.html.erb
dancing.html.erb
programming.html.erb
reading.html.erb
running.html.erb
sports.html.erb
writing.html.erbmatch '/adventure', to: 'pages#adventure', via: 'get'
match '/cooking', to: 'pages#cooking', via: 'get'
match '/dancing', to: 'pages#dancing', via: 'get'
match '/programming', to: 'pages#programming', via: 'get'
match '/reading', to: 'pages#reading', via: 'get'
match '/running', to: 'pages#running', via: 'get'
match '/sports', to: 'pages#sports', via: 'get'
match '/writing', to: 'pages#writing', via: 'get'%w(adventure cooking dancing programming reading running sports writing).each do |page|
match "/#{page}", to: "pages##{page}", via: 'get'
endContext
StackExchange Code Review Q#59825, answer score: 8
Revisions (0)
No revisions yet.