简体   繁体   English

Ruby on Rails 'link_to' 方法创建错误的链接

[英]Ruby on Rails 'link_to' method creates wrong link

I am having issues with the link_to method in Rails.我在 Rails 中遇到了link_to方法的问题。 I have the routes established, but the urls aren't working correctly.我已经建立了路线,但网址无法正常工作。

3000/gov_official => my root page

3000/gov_official/1 => desired show page url

3000/gov_official.1 => what I am getting...

Any help would be greatly appreciated.任何帮助将不胜感激。

My code snippet:我的代码片段:

在此处输入图片说明

在您的路线中尝试以下操作,而不是手动定义索引/显示路线。

resource: :gov_official, only: [:get]

可能有问题link_to,试试这个。

<%= link_to gov.name, gov_official_path(id: gov.id) %>

From my own experience, the problem is that Rails can't figure out which path are you trying to link to.根据我自己的经验,问题是 Rails 无法确定您要链接到哪条路径。 Because, your resource is not in plural form gov_officials , you don't have 2 clearly separate paths:因为,您的资源不是复数形式gov_officials ,您没有 2 条明确分开的路径:

  1. gov_officials_path - takes 1 argument format . gov_officials_path - 采用 1 个参数格式 Generates: gov_officials.format .生成: gov_officials.format
  2. gov_official_path - takes 1 argument model . gov_official_path - 采用 1 个参数模型 Generates: gov_officials/:id生成: gov_officials/:id

So, to solve your problem the Rails way , use pluralization of your resources correctly.因此,要以 Rails 方式解决您的问题,请正确使用资源的多元化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM