1

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.

moosefetcher
  • 1,841
  • 2
  • 23
  • 39
  • I'd turn them into a pages resource, add in a simple admin system and make the pages editable in the admin system. – j-dexx Jan 14 '15 at 15:10

4 Answers4

0

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

Stanislav Mekhonoshin
  • 4,276
  • 2
  • 20
  • 25
0

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
Community
  • 1
  • 1
gkolan
  • 1,571
  • 2
  • 20
  • 37
0

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
Jacob Brown
  • 7,221
  • 4
  • 30
  • 50
  • There's obviously some details about routing I don't yet understand. If you wouldn't mind explaining; why would your solution mean 'losing named routes' and what does that mean? – moosefetcher Jan 15 '15 at 10:52
  • @moosefetcher, routes defined normally in `routes.rb` will generate a named route helper which can be used in other parts of your application (e.g., `link_to "E-learning", elearning_path`). The first example in my answer does the routing dynamically, so you wouldn't be able to use `elearning_path`, you would have to use something like `url_for(controller: 'home', action: 'elearning')`. This only applies to the first example here. For more on Rails routing, I strongly recommend http://guides.rubyonrails.org/routing.html, which I revisit all the time. – Jacob Brown Jan 15 '15 at 12:39
  • I have been directed to that page so often, I can't tell you. I'll give it yet another look, but the Rails methods (eg url_for) do just seem to come out of nowhere. Under which class is the url_for method listed in the docs? – moosefetcher Jan 15 '15 at 13:30
0

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!

Tall Paul
  • 2,398
  • 3
  • 28
  • 34