【问题标题】:Easy Rails link_toEasy Rails 链接到
【发布时间】:2015-10-09 01:27:10
【问题描述】:

我正在遍历一组结果,我的代码中有这个link_to

<%= link_to "", "#{expo.id}/edit" %>

我预计它会转到&lt;domain&gt;/exhibitions/3/edit。 我已经在/exhibitions 页面中,所以这就是我从中得到的。问题是,它转到&lt;domain&gt;/exhibitions/3 并在那里结束。由于某种原因,它不会在 URL 的 edit 部分上添加标签。任何人都可以帮忙吗?谢谢!

更新

代码是这样包裹的:

<% @exhibition.each do |expo| %>
    <%= link_to "", expo_edit_path(:id) %>
<% end %>

更新 2

我运行了rake routes,但我确实有不正确的编辑路径。现在的问题是 URL。 URL 从字面上变成:http://localhost:3000/exhibitions/id/edit 它字面上写出 id 这个词。

这是我的新link_to 声明:&lt;%= link_to "", edit_exhibition_path(:id) %&gt;

错误如下:

ActiveRecord::RecordNotFound in ExhibitionsController#edit 找不到“id”=id 的展览

【问题讨论】:

  • 嗯,我认为您的两个部分在编辑后现在不同步了。

标签: ruby-on-rails url routing routes link-to


【解决方案1】:

使用类似的东西

link_to "text", edit_expo_path(expo)

您应该使用 config/routes.rb 中定义的路由 - 阅读 rails routing 以查看格式。

您可能会发现https://stackoverflow.com/a/12141379/631619 很有帮助。
还有https://stackoverflow.com/a/29419670/631619

【讨论】:

  • 我仍然收到此错误:undefined method "expo_edit_path" for #&lt;#&lt;Class:0x007ff753602be0&gt;:0x007ff74d83c5b0&gt;
  • 已更正。也请rake routes 看看存在哪些路径。您可能需要添加它。在您的问题中发布结果。
  • 您需要将 expo 对象或 expo.id 放在那里。
  • 我正在寻找一份兼职的 RoR 工作。你有什么要提供的吗?一旦你看到它,我会删除我的评论..
猜你喜欢
  • 1970-01-01
  • 2023-04-04
  • 1970-01-01
  • 2018-11-13
  • 2013-06-12
  • 1970-01-01
  • 1970-01-01
  • 2021-10-26
  • 1970-01-01
相关资源
最近更新 更多