嵌套资源的Rails 3路由错误 [英] Rails 3 Routing Error with Nested Resources

查看:77
本文介绍了嵌套资源的Rails 3路由错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Rails应用程序中,有很多游戏,每个游戏都有自己的排行榜。然后,将排行榜嵌套在游戏中就很有意义,因此您只能通过游戏进入排行榜。我这样设置了route.rb文件(重要部分):

In my Rails application, there are many games, and each game has it's own set of leaderboards. It makes sense then, to have the leaderboards nested in the game, so you can only get to a leaderboard through a game. I setup my routes.rb file as such (the important part):

resources :games do
  resources :leaderboards
end

然后我更新了控制器,以便从game_id中获取合适的游戏传入,并从中获取排行榜信息。但是,我的问题来自我的观点。在本节中(从脚手架自动生成的视图):

So then I updated my controller so it would get the appropriate game from the game_id passed in, and grab the leaderboard information from that. However, my issues comes from my view. In this section (auto generated view from the scaffold):

<% @leaderboards.each do |leaderboard| %>
  <tr>
    <td><%= leaderboard.name %></td>
    <td><%= leaderboard.scoreColumnName %></td>
    <td><%= leaderboard.game_id %></td>
    <td><%= link_to 'Show', [@game, leaderboard] %></td>
    <td><%= link_to 'Edit', edit_game_leaderboard_path(leaderboard) %></td>
    <td><%= link_to 'Destroy', [@game, leaderboard], :confirm => 'Are you sure?', :method => :delete %></td>
  </tr>
<% end %>
</table>

代码中断说:

No route matches {:action=>"edit", :controller=>"leaderboards", :game_id=>#<Leaderboard id: 1, name: "Test High Score Leaderboard", scoreColumnName: "Score", game_id: 1, created_at: "2011-07-03 01:32:33", updated_at: "2011-07-03 01:32:33">}

这一行,原来是错误:(我的代码中的第19行)

This line, it turns out is the error: (line 19 in my code)

<td><%= link_to 'Edit', edit_game_leaderboard_path(leaderboard) %></td>

删除此行,视图即可正常显示。因此,URL部分已损坏,但是如何解决?奇怪的是,我在显示视图中有确切的 edit_game_leaderboard_path,并且工作正常……我在做什么错了?

Removing this line, and the view renders fine. So, the URL part is broken, but how do I fix it? The weird thing is, I have that exact "edit_game_leaderboard_path" in the Show view, and it works fine... what am I doing wrong?

推荐答案

您要:

<%= link_to 'Edit', edit_game_leaderboard_path(@game, leaderboard) %>

这篇关于嵌套资源的Rails 3路由错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆