I've been told the routes.rb file I've put together looks like it was coded in a panic (it was coded in a panic).
The Rails app I'm developing is an e-learning and not all pages involve quizzing a database.
How do I go about including those pages? Do I NOT use routes at all? Or should pages be a resource? I'm a bit lost about this.
The tutorials all seem to focus on the RESTful interactions and not on the behaviour of the site in general.
Here's my routes file:
Rails.application.routes.draw do
devise_for :users
root :to => 'home#index'
get "elearning" => "home#elearning"
get "howitworks" => "home#howitworks"
get "leadershipstyles" => "home#leadershipstyles"
get "whattypeofleader" => "home#whattypeofleader"
get "showmetheskills" => "home#showmetheskills"
get "safeguarding" => "home#safeguarding"
get "toolsforthejob" => "home#toolsforthejob"
get "whatnext" => "home#whatnext"
get "congratulations" => "home#congratulations"
get "teamwork" => "home#teamwork"
get "presentation" => "home#presentation"
get "planning" => "home#planning"
get "communication" => "home#communication"
# get "newjournal" => "posts#new"
# get "profile" => 'users#show'
# get "profile/edit" => 'users#edit'
resources :users do
resources :posts
end
end
OK, so there's a few pages under the /home route. Is that the right way to go about it? If you could explain a better alternative, I'd appreciate it.
You can create resource page, for example. It will deal with model, containing page content.
With such approach you will clean up your routes file
Check this answer
resources :home, path:'', except: [:new, :create, :edit, :update, :destroy] do
collection do
get 'elearning'
get 'howitworks'
get 'whattypeofleader'
......
......
get 'communication'
end
end
If you don't mind losing named routes, and don't want to list all of the possible actions, you can do:
get ':action', controller: 'home'
which will route all unmatched requests matching this pattern to the Home
controller. You'll want to put this towards the bottom in routes.rb
so it doesn't intercept other requests.
Otherwise, you can do:
scope module: 'home' do
get 'elearning'
# ...
end
Or even:
scope module: 'home' do
%w{ elearning howitworks etc }.each do |r|
get r
end
end
So I found a cool snippet of code I use with my static pages.
I set up a static page controller and all you need to do is in there put the name of the page. Then under the static page view folder you add a file with that name and in routes.rb all you have to do is
StaticPagesController.action_methods.each do |action|
get "/#{action}", to: "static_pages##{action}", as: "#{action}"
end
This iterates over the controller you get and creates the routes for you.
If you want to see an example of this you can check out this simple blogger that I did with it
The Controller
The View
The Routes
Hope that helps!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.