问题
I have made a forum-based website, I want to add a few links to my footer like "more about badges" or "how to ask questions". Well, they are static pages so I really don't want to go through controller to go to those pages. For example, I'd like to view views/static_pages/badges.html.erb
using link_to
or other possible tag.
How can I go directly to some page in my views?
回答1:
For static pages you should check out high_voltage gem. Once it's installed, it'll make a folder pages
inside your view directory and it'll generate a named route method of page_path
. For example, if you create a static page pages/static
then you can refer it to by page_path('static')
in your views.
EDIT:
If you don't want to use any gem then I'll suggest checking out this question
回答2:
We do it like this:
#config/routes.rb
resources :pages, only: [:show] #-> sends params[:id] as /page_name
#app/controllers/pages_controller.rb
Class PagesController < ApplicationController
def show
render "pages/#{params[:id]}"
end
end
This will allow you to call the following:
= link_to pages_path("badges") # -> domain.com:3000/pages/badges
来源:https://stackoverflow.com/questions/24095215/link-to-for-static-pages