문제

This is my first Rails project, I am trying to piece things together slowly.

When I'm trying to view the page I generated using rails g controller <controller> <page>, I find myself going to 0.0.0.0:3000/controller/page.html, How can I configure it so that my route file globally allows viewing the page via the page name, rather than controller/page, if no such way exists, then how can I route controller/page.html to /page.html

I've looked around, and haven't really found any explanation, maybe I'm looking in the wrong places?

도움이 되었습니까?

해결책

In config/routes.rb:

get '/page' => 'controller#action'

If your controller is:

class UsersController < ApplicationController
  def something
  end
end

Then config/routes.rb would be:

get '/page' => 'users#something'

For static pages you could want to use public folder though, everything you put there is directly accessible, for example public/qqqqqq.html would be accessed in localhost:3000/qqqqqq.html

다른 팁

We've just achieved this by using the path argument in resources method:

#config/routes.rb
resources :controller, path: ""

For you specifically, you'll want to make something like this:

#config/routes.rb
resources :static_pages, path: "", only: [:index]
   get :page
   get :other_page
end

#app/controllers/your_controller.rb
def page
end

def other_page
end

This will give you routes without the controller name. You'll have to define this at the end of your routes (so other paths come first)

Obviously this will form part of a wider routes file, so if it doesn't work straight up, we can refactor!

It sounds like this is a static page, so you can do as juanpastas says, or another option is to create a folder under your app/views directory to hold these pages. Maybe something like app/views/static_pages/the_page.html.erb

Then in your config/routes.rb you can add:

match '/your_page_name', to: 'static_pages#the_page', via: :get

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top