2017-01-02 1 views
0

프리픽스가 필요없는 가상 URL을 지원하도록 routes.rb를 설정하려고합니다. 예를 들어 mysite.com/articles/seo-url-here에 'articles /'가 필요하지 않은 경우입니다. 내가 원하는 단지Rails routes.rb를 설치하는 방법 프리픽스가 필요없는 허영 URL

mysite.com/seo-url-here 내가 어떻게 설정 routes.rb URL에 내 사이트를 칠 때 너무 : routes.rb URL에 seo-url-here의 값이 테이블 Article.seo_url 내 데이터베이스의 기록과 일치하는지 보인다. match가 없으면 routes.rb는 routes.rb 파일의 나머지 부분을 아래로 이동해야합니다.

감사

얻을

답변

1

기본 코드는 시작 :

# config/routes.rb 

Rails.application.routes.draw do 
    resources :posts 
    resources :authors 

    constraints(PostUrlConstrainer.new) do 
    get "/:id", to: "posts#show" 
    end 
end 

# app/constraints/post_url_constrainer.rb 

class PostUrlConstrainer 
    def matches?(request) 
    title = request.path_parameters[:id] 
    Post.find_by(title: title) 
    end 
end 

# app/controllers/posts_controller.rb 

def set_post 
    @post = Post.find_by(title: params[:id]) 
end 

관련 기사

: rails url constraint 도움이 보인다 위해 Pretty, short urls for every route in your Rails app - Arkency Blog

검색.

관련 문제