Rails 3 嵌套资源的路由错误

发布于 2024-11-18 17:29:24 字数 1438 浏览 4 评论 0原文

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

resources :games do
  resources :leaderboards
end

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

<% @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 行)

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

删除这一行,视图呈现良好。那么,URL 部分已损坏,但如何修复呢?奇怪的是,我在“显示”视图中有确切的“edit_game_leaderboard_path”,并且它工作正常......我做错了什么?

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

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>

The code breaks saying:

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">}

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

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

在你怀里撒娇 2024-11-25 17:29:24

你想要:

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

You want:

<%= link_to 'Edit', edit_game_leaderboard_path(@game, leaderboard) %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文