在導軌,用於不同的路線助手的名字應該是不同的。因此,正如你理解的那樣,如果你爲兩條不同的路線使用相同的助手名字(例如你的案例中的'profile'),你會收到一個錯誤。
Rails中的這個限制有助於在您的routes.rb文件以及您的應用程序中保持完整性。例如考慮兩種方式,一種是控制器:
class XyzController < ApplicationController
def method_a(param1)
end
def method_b(param1)
end
end
在你的路由文件,如果沒有保持幫手名不同的限制,你也可以使用:
get 'xyz/method_a', to: 'profile#method_a', as: 'profile_method'
get 'xyz/method_b', to: 'profile#method_b', as: 'profile_method'
相應的,在您的視圖文件:
link_to 'link_1', profile_method_path('param1') #intended to route for method_a
link_to 'link_2', profile_method_path('param2') #intended to route for method_b
顯而易見,在視圖文件中,不僅難以確定哪種路由適用於哪種方法,也不可能路由到任何其他控制呃使用助手'profile_method'的方法,除了首先在你的routes.rb文件中使用這個助手的方法(因爲路由是按順序讀取的)。
希望這會有所幫助:)
爲什麼不將「slug」參數定義爲可選項,然後您可以將兩者合併?當然,在控制器中有一些邏輯。我認爲你提到的錯誤有一個理由,顯然這兩個行爲是針對不同的事情的,爲什麼他們必須有相同的名字? – 2014-11-05 18:23:19