开发者

RoR: How do I configure my routes to view all files in a folder?

开发者 https://www.devze.com 2023-02-28 11:20 出处:网络
This is my directory structure, and when I try to do home/demo I see \"Page does not exist\" ├───app

This is my directory structure, and when I try to do home/demo I see "Page does not exist"

├───app
│   ├───controllers
│   ├───helpers
│   └───views
│       ├───home
             demo.html.erb
             开发者_如何学Gohelloworld.html.erb
             helloworld_sid.html.erb
             index.html.erb
             spinner.html.erb
...

Is there an easier way to configure the routes, so I don't have to do this in my routes.rb?

  get "home/demo"
  get "home/helloworld"
  get "home/helloworld_sid"
  get "home/spinner"

Tried: get "home/*", "home", "home/".

Thanks!


This should do the trick:

match '/home/:action', :controller => 'home'


Here is how I'd proceed:

scope "home" do
  match "/"      => "home#index", :as => 'home_index'
  match "/hello" => "home#hello", :as => 'home_hello'
end


You'll want to have a HomeController (although it's standard practice to pluralize, odd though "HomesController" may sound) with an action (e.g. #show) that accepts the page name as a param (e.g. :id) and renders the proper template.

I use https://github.com/thoughtbot/high_voltage as a reusable solution to this, or you can check out the source and write your own.

0

精彩评论

暂无评论...
验证码 换一张
取 消