使用slugs显示动作的自定义路线

时间:2014-01-07 23:28:37

标签: ruby-on-rails routes slug friendly-id

我正在尝试为我想要显示的模型创建一些网址。我有文章,属于属于问题的部分。

我希望我的文章的网址显示操作如下:

/issue-slug/section-slug/article-slug问题文章和章节都有存储在db中的slu。

现在我有一个名为'pressroom'的后端部分,我有以下路线。这是整个routes.rb文件

MaskmagazineCom::Application.routes.draw do

 devise_for :users, :path_names => { :sign_up => "register"}, :controllers => {    :registrations => "registrations" }
 # The priority is based upon order of creation: first created -> highest priority.
 # See how all your routes lay out with "rake routes".

 root 'magazine#index'
 get 'users/' => 'users#index'


 # Lobby Routes

 # /log-in

 devise_scope :user do
 get '/sign-in' => 'devise/sessions#new'
end

 # /subscribe

 get 'subscribe' => 'subscribe#stepone'
 get 'subscribe/sliding-scale' => 'subscribe#steptwo'
 get 'subscribe/sliding-scale/subscriber' => 'subscribe#subscriber'
 get 'subscribe/sliding-scale/supporter' => 'subscribe#supporter'
 get 'subscribe/sliding-scale/sustainer' => 'subscribe#sustainer'
 post 'subscribe/sliding-scale/:type' => 'subscribe#createSubscription'



# Pressroom Routes

get '/pressroom' => 'pressroom#index'
scope 'pressroom' do
resources :issues, :articles, :sections, :users, :authors
end

如何取出show动作并将其路由到我描述的网址?

编辑:

我想出了我想在路线文件中做的事情,但我需要相应的控制器代码:

get '/:issue_slug/:section_slug/:article_slug' => 'article#show'

1 个答案:

答案 0 :(得分:0)

我建议您查看friendly_id以查找slu ..

对于路线,您需要阅读路线:

# Mag Routes

get '/mag' => 'mag#index' #or wherever you're headed
  scope 'mag' do
    resources :issues do
      resources :sections do
        resources :articles
      end
    end
  end
end