Link_to for static pages

让人想犯罪 __ 提交于 2020-01-14 05:00:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!