2013-07-20 56 views
0

我想使用link_to重定向選擇顯示頁面。 但是,當我點擊這個鏈接它重定向到根頁面。鏈接重定向爲根目錄而不是控制器顯示頁面

我嘗試了不同的方式,但它總是重定向到索引根。

<%= link_to 'Detalhes', selection %> 

我也試過:

<%= link_to 'Detalhes', selection_path(selection) %> 
<%= link_to 'Detalhes', selection, :method => :show %> 
<%= link_to 'Detalhes', selection, method: :show %> 

而且,在routes.rb

match '/selections/:id' => 'selections#show', :via => :post 

路線,如果需要的是:

selections GET /selections(.:format)         selections#index 
        POST /selections(.:format)         selections#create 
     new_selection GET /selections/new(.:format)        selections#new 
     edit_selection GET /selections/:id/edit(.:format)       selections#edit 
      selection GET /selections/:id(.:format)        selections#show 
        PUT /selections/:id(.:format)        selections#update 
        DELETE /selections/:id(.:format)        selections#destroy 

我的日誌,當我嘗試訪問選擇:

Started GET "/selections/1" for 127.0.0.1 at 2013-07-21 09:34:55 -0300 
Processing by SelectionsController#show as HTML 
Parameters: {"id"=>"1"} 
[1m[35mSelection Load (0.2ms)[0m SELECT "selections".* FROM "selections" WHERE  "selections"."id" = ? LIMIT 1 [["id", "1"]] 
    [1m[36mUser Load (0.3ms)[0m [1mSELECT "users".* FROM "users" WHERE "users"."id" = 2 LIMIT 1[0m 
    [1m[35mRole Load (0.4ms)[0m SELECT "roles".* FROM "roles" INNER JOIN "roles_users" ON "roles"."id" = "roles_users"."role_id" WHERE "roles_users"."user_id" = 2 AND "roles"."name" = 'Administrator' LIMIT 1 
    [1m[36mRole Load (0.4ms)[0m [1mSELECT "roles".* FROM "roles" INNER JOIN "roles_users" ON "roles"."id" = "roles_users"."role_id" WHERE "roles_users"."user_id" = 2 AND "roles"."name" = 'TeamMaster' LIMIT 1[0m 
    [1m[35mRole Load (0.4ms)[0m SELECT "roles".* FROM "roles" INNER JOIN "roles_users" ON "roles"."id" = "roles_users"."role_id" WHERE "roles_users"."user_id" = 2 AND "roles"."name" = 'Team' LIMIT 1 
    [1m[36mCACHE (0.0ms)[0m [1mSELECT "selections".* FROM "selections" WHERE "selections"."id" = ? LIMIT 1[0m [["id", "1"]] 
    Rendered selections/show.html.erb within layouts/application (1.7ms) 
Redirected to http:// localhost:3000/ 
Completed 302 Found in 34ms (ActiveRecord: 0.0ms) 
+0

你不能有這行'match'/ selections /:id'=>'selections#show',:via =>:post'因爲它沒有顯示在你的路線中(或者你沒有包含它)。這條線本身毫無意義,因爲您希望應用程序如何在POST請求上顯示某些內容? –

+0

在這一行中,<%= link_to'單獨',選擇%>'你作爲'選擇'通過了什麼? –

+0

你是對的邁克爾Szyndel,這只是一個絕望的嘗試。 並且在<%= link_to'單元',選擇%>中,選擇是所有選擇索引的列表,其結果是@ selections.each do | selection | –

回答

0

的正確實施是有:在你的的routes.rb

resources :selections

在你的意見

link_to 'The text', selection

驗證的事情是:

  • 您重新啓動服務器時更改route.rb之後,否則它不會追上它
  • 在您看來,如果它的正確
  • 後檢查鏈接的HREF點擊檢查你的滑軌控制檯,看看是否沒有重定向
+0

嗨,我忘了一件事: 在routes.rb我試過「資源:選擇」,並沒有工作。 當我嘗試過「match」/ selections /:id'=>'selections#show',:via =>:post「之後,我得到了相同的結果。 因此,顯然,routes.rb中的路由不是問題。 –

+0

什麼是生成的href? – Benj

相關問題