2016-05-24 32 views
1

爲什麼鐵軌正在像什麼時候rails使用點來代替反斜槓?

/notification_templates/duplicate_me.1 

路徑它應該是

/notification_templates/duplicate_me/1 

我的路線是

resources :notification_templates do 
    collection do 
     get :blast_send 
     patch :deactivate 
     patch :activate 
     get :get_list 
     post :duplicate_me 
    end 
    end 

和我的鏈接是 <%= link_to "Duplicate", duplicate_me_notification_templates_path(template), method: :post, class: "btn btn-primary" %>

+1

'/'=斜槓,'''''=反斜槓 – Stefan

+0

耶謝謝指出 – Prem

回答

1

由於路線是爲了收集它並不期望id的模板實例變量。

爲了使路線爲/notification_templates/duplicate_me/1,使你的routes.rb

resources :notification_templates do 
    collection do 
    get :blast_send 
    patch :deactivate 
    patch :activate 
    get :get_list 
    end 
    post :duplicate_me, on: :member 
end 

的變化,改變你的view

<%= link_to "Duplicate", duplicate_me_notification_template_path(template), method: :post, class: "btn btn-primary" %> 
2

您正嘗試將資源傳遞給集合路線。對於這個工作,你的路線應該被定義爲一個member route代替:

resources :notification_templates do 
    collection do 
    get :blast_send 
    patch :deactivate 
    patch :activate 
    get :get_list 
    end 
    member do 
    post :duplicate_me 
    end 
end 

其原因將其轉換路線爲點目前是路徑幫手最有可能瞭解到在(template)傳遞的參數作爲format specification。使用點將格式與路線分開。

0

當你要像

/notification_templates/duplicate_me/1

通過ID作爲成員,你應該讓成員路由資源。請看這enter link description here。它會幫助你。

這樣可以幫到你。

 
resources :notification_templates do 
    collection do 
    get :blast_send 
    patch :deactivate 
    patch :activate 
    get :get_list 
    end 
    member do 
    post :duplicate_me 
    end 
end