0

I am trying to setup a new static and later dynamic page.

Inside app/controllers/ I created detailpages_controller.rb. Inside I have:

class DetailPagesController < ApplicationController    
 def show
  render
 end
end

Then, on config/routes.rbI have:

Rails.application.routes.draw do
   root 'welcome#index'
   DetailPagesController.action_methods.each do |action|
    get "/#{action}", to: "detailpages##{action}", as: "#{action}_page"
  end
end

On app/viewes/pages there is a detailpages.html.erb file that just contains a <h2>Hello World</h2>

When I go to http://localhost:3000/detailpages.html I get:

No route matches [GET] "/detailpages.html"

if I just do localhost:3000 my index.html works perfectly fine but I can't, for my life, add this new page so I can later link to it.

Could someone please tell me what I'm doing wrong?

Hizqeel
  • 947
  • 3
  • 20
  • 23
  • Why are you looping through the action methods? – Eyeslandic Feb 11 '17 at 15:37
  • I read somewhere that it's smarter than adding a new row manually everytime I get a new action. I will have a few, but I can't get this one to begin with. –  Feb 11 '17 at 15:38
  • Well, what is your objective with this controller? If you just want to map this static page to the show action you can do `get 'detailpages.html' => 'detail_pages#show', as: :detail_page` – Eyeslandic Feb 11 '17 at 15:40
  • I still get an error, just a different one: `uninitialized constant DetailPagesController` –  Feb 11 '17 at 16:00
  • The name of the file is wrong, should be `detail_pages_controller.rb` – Eyeslandic Feb 11 '17 at 16:02
  • oh ok, that changed the error entirely. So, 2 questions: 1) do I need to always separate words by _ if the Controller has more than 1 word? (i.e. DetailPages = detail_pages and 2) now I have `Missing template detail_pages/show` I thought with a method `def show render :action => :show end` it would be enough but I'm missing something, could you point me to docs that talk about this? And thank you! –  Feb 11 '17 at 16:07
  • Yes, each word is separated with `_`, called snake case if I remember correctly. You will need to add the template `app/views/detail_pages/show.html.erb` – Eyeslandic Feb 11 '17 at 16:09
  • http://guides.rubyonrails.org/layouts_and_rendering.html – Eyeslandic Feb 11 '17 at 16:09
  • 1
    got it to work. Thank you! If you want to write it as an answer, I'll mark you as the right one –  Feb 11 '17 at 16:14

1 Answers1

0

You can map to the detailpages.html with

get 'detailpages.html' => 'detail_pages#show', as: :detail_page

Also you need to edit the name of your DetailPagesController, it should be detail_pages_controller.rb. Every word is separated with _, called snake casing.

You will also need a corresponding view, placed in app/views/detail_pages/show.html.erb

Eyeslandic
  • 14,553
  • 13
  • 41
  • 54