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)
4

1 回答 1

0

正确的实现是:

在你的routes.rb

resources :selections

在你看来

link_to 'The text', selection

要验证的事情是:

  • 更改 route.rb 后您重新启动了服务器,否则它不会赶上它
  • 在您看来,检查链接的 HREF 是否正确
  • 点击后检查你的rails控制台,看看是否没有重定向
于 2013-07-20T23:16:21.787 回答