簡體   English   中英

Rails 4路由名稱空間/有關url變量或參數的問題

[英]Rails 4 routes namespace / concern with url variables or parameters

我有一個應用程序,該應用程序的日歷部分有一些煩人的特定路由。 他們看起來像這樣:

MyApp::Application.routes.draw do
  ...
  day_constraints = { year: /\d{4}/, month: /\d{1,2}/, day: /\d{1,2}/ }
  get 'days/:month/:day/:year', to: 'schedule#day', constraints: day_constraints, as: :schedule_day
  get 'days/:month/:day/:year/print', to: 'schedule#day_print', constraints: day_constraints
  get 'days/:month/:day/:year/route', to: 'routes#index', constraints: day_constraints
  ...
end

如您所見,這里有很多重復項。 他們都路由到計划控制器。 我想知道是否有減少重復的方法。 我在想一個命名空間或類似的問題:

MyApp::Application.routes.draw do
  ...
  day_constraints = { year: /\d{4}/, month: /\d{1,2}/, day: /\d{1,2}/ }
  namespace 'days/:month/:day/:year' constraints: day_contstraints do
    get 'print', to: 'schedule#day_print'
    get 'route', to: 'routes#index'

    root to: 'schedule#day'
  end
  ...
end

但這會引發錯誤:

'day/:month/:day/:year/schedule' is not a supported controller name. This can lead to potential routing problems.

有什么建議如何清理呢?

嘗試使用scope而不是namespaceget "/" => "schedule#day"代替root to: 'schedule#day'

day_constraints = { year: /\d{4}/, month: /\d{1,2}/, day: /\d{1,2}/ }
scope 'days/:month/:day/:year', constraints: day_constraints do
  get 'print', to: 'schedule#day_print'
  get 'route', to: 'routes#index'
  get '/', to: 'schedule#day'
end

我還必須在范圍和約束之間添加逗號。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM