2013-12-22 32 views
0

如果我把這個在route.rb get '/products/:id', to: 'products#edit'編輯操作應該被調用時url格式爲/patients/:id這是否意味着它將被重定向到編輯頁面?我試過這個,它不是重定向。從代碼導軌生成路徑和URL

這是我的耙路輸出

admin GET /admin(.:format)        {:action=>"index", :controller=>"admin"} 
        GET /products/:id(.:format)      {:controller=>"products", :action=>"edit"} 
      login GET /login(.:format)        {:action=>"new", :controller=>"sessions"} 
        POST /login(.:format)        {:action=>"create", :controller=>"sessions"} 
      logout DELETE /logout(.:format)       {:action=>"destroy", :controller=>"sessions"} 
      users GET (/:locale)/users(.:format)     {:action=>"index", :controller=>"users"} 
        POST (/:locale)/users(.:format)     {:action=>"create", :controller=>"users"} 
      new_user GET (/:locale)/users/new(.:format)    {:action=>"new", :controller=>"users"} 
     edit_user GET (/:locale)/users/:id/edit(.:format)   {:action=>"edit", :controller=>"users"} 
       user GET (/:locale)/users/:id(.:format)    {:action=>"show", :controller=>"users"} 
        PUT (/:locale)/users/:id(.:format)    {:action=>"update", :controller=>"users"} 
        DELETE (/:locale)/users/:id(.:format)    {:action=>"destroy", :controller=>"users"} 
      orders GET (/:locale)/orders(.:format)     {:action=>"index", :controller=>"orders"} 
        POST (/:locale)/orders(.:format)     {:action=>"create", :controller=>"orders"} 
     new_order GET (/:locale)/orders/new(.:format)    {:action=>"new", :controller=>"orders"} 
     edit_order GET (/:locale)/orders/:id/edit(.:format)   {:action=>"edit", :controller=>"orders"} 
      order GET (/:locale)/orders/:id(.:format)    {:action=>"show", :controller=>"orders"} 
        PUT (/:locale)/orders/:id(.:format)    {:action=>"update", :controller=>"orders"} 
        DELETE (/:locale)/orders/:id(.:format)    {:action=>"destroy", :controller=>"orders"} 
     line_items GET (/:locale)/line_items(.:format)    {:action=>"index", :controller=>"line_items"} 
        POST (/:locale)/line_items(.:format)    {:action=>"create", :controller=>"line_items"} 
    new_line_item GET (/:locale)/line_items/new(.:format)   {:action=>"new", :controller=>"line_items"} 
    edit_line_item GET (/:locale)/line_items/:id/edit(.:format)  {:action=>"edit", :controller=>"line_items"} 
     line_item GET (/:locale)/line_items/:id(.:format)   {:action=>"show", :controller=>"line_items"} 
        PUT (/:locale)/line_items/:id(.:format)   {:action=>"update", :controller=>"line_items"} 
        DELETE (/:locale)/line_items/:id(.:format)   {:action=>"destroy", :controller=>"line_items"} 
      carts GET (/:locale)/carts(.:format)     {:action=>"index", :controller=>"carts"} 
        POST (/:locale)/carts(.:format)     {:action=>"create", :controller=>"carts"} 
      new_cart GET (/:locale)/carts/new(.:format)    {:action=>"new", :controller=>"carts"} 
     edit_cart GET (/:locale)/carts/:id/edit(.:format)   {:action=>"edit", :controller=>"carts"} 
       cart GET (/:locale)/carts/:id(.:format)    {:action=>"show", :controller=>"carts"} 
        PUT (/:locale)/carts/:id(.:format)    {:action=>"update", :controller=>"carts"} 
        DELETE (/:locale)/carts/:id(.:format)    {:action=>"destroy", :controller=>"carts"} 
who_bought_product GET (/:locale)/products/:id/who_bought(.:format) {:action=>"who_bought", :controller=>"products"} 
      products GET (/:locale)/products(.:format)    {:action=>"index", :controller=>"products"} 
        POST (/:locale)/products(.:format)    {:action=>"create", :controller=>"products"} 
     new_product GET (/:locale)/products/new(.:format)   {:action=>"new", :controller=>"products"} 
     edit_product GET (/:locale)/products/:id/edit(.:format)  {:action=>"edit", :controller=>"products"} 
      product GET (/:locale)/products/:id(.:format)   {:action=>"show", :controller=>"products"} 
        PUT (/:locale)/products/:id(.:format)   {:action=>"update", :controller=>"products"} 
        DELETE (/:locale)/products/:id(.:format)   {:action=>"destroy", :controller=>"products"} 
      store  /(:locale)(.:format)       {:controller=>"store", :action=>"index"} 
+0

你怎麼重定向到編輯頁面? 'redirect_to'或'link_to'? – swapab

+0

這不是重定向。如果您通過使用redirect_to從一個動作向另一個動作發送請求而不是其調用重定向,那麼您正在嘗試的是將此URL映射到患者控制器的編輯動作 –

+0

我鼓勵您閱讀[rails routing](http:// guides .rubyonrails.org/routing.html) – swapab

回答

0

你到了那裏沒有做重定向路由,它的URL /patients/:id映射到PatientsControlleredit作用。

當用戶點擊/patients/id url時,將調用edit操作。默認情況下,這會嘗試渲染一個與動作同名的視圖。

如果要渲染編輯頁面,請在名爲edit.html.erb/app/views/patients文件夾中創建一個視圖。

(順便說一句,在GET /model/:id路由通常用於show動作。)

+0

我知道它是用於顯示動作,但我正在學習rails,並且想嘗試一下。我已經有了edit.html.erb,但沒有渲染它。顯示操作在這裏被調用。 – asdfkjasdfjk

+0

你可以將'rake routes'的輸出添加到你的問題中嗎? –

+0

添加了耙路線 – asdfkjasdfjk