0
我有一個控制器SubscriptionsController
只有動作new
和create
。如果有人試圖訪問GET /subscriptions
,我將如何重定向到new
,這通常會觸發index
操作?如何在Rails中ActionNotFound時重定向
的config/routes.rb中
resource :subscriptions, :only => [:new, :create]
我有一個控制器SubscriptionsController
只有動作new
和create
。如果有人試圖訪問GET /subscriptions
,我將如何重定向到new
,這通常會觸發index
操作?如何在Rails中ActionNotFound時重定向
的config/routes.rb中
resource :subscriptions, :only => [:new, :create]
使用Rails3中,你可以從一個路線做,是這樣的:
match "/subscriptions", :to => redirect("/subscriptions/new")
編輯:
從它的意見明確表示要捕獲更多,使用通配符可以使其更通用。您可能需要將此表單與前一個表單結合處理非斜槓表單(或者嘗試下面的表單而不用斜槓,我沒有嘗試過)。還要確保將這些「全部捕捉」路線放在其他路線下方,因爲路線是從上到下匹配的。
match "/subscriptions/*other", :to => redirect("/subscriptions/new")
當然,我可以硬編碼。但是接下來我必須爲'/ subscrptions/1'和'/ subscriptions/1/edit'等所有PUT和DELETE請求執行此操作。基本上,我想爲所有未找到路由的「全部抓取」,重定向到「預訂#new' –
'match「/ subscriptions/* other」'並放置在其他路線之後。 – ctcherry
謝謝!這裏是我作爲最後一條路線匹配* other',:to =>重定向('/')' –