[英]Rails routing issue, can't figure out the link path
Let me fair from the outset, and tell you that I've 'solved' the problem I'm describing. 让我从一开始就公平,并告诉您我已经“解决”了我描述的问题。 But a solution that you don't understand is not really a solution, now is it?
但是您不了解的解决方案并不是真正的解决方案,是吗?
I have a resource, Newsbites. 我有资源,新闻快讯。 I have an index page for Newsbites.
我有一个Newsbites的索引页。 All my CRUD actions work fine.
我所有的CRUD动作均正常。
I created a separate index ( frontindex.html.erb
) that acts as the front page of my website to show the newest Newsbites. 我创建了一个单独的索引(
frontindex.html.erb
),用作我网站的首页,以显示最新的新闻快讯。 The formatting is different from my normal index so readers get a larger photo, more of the text of the article(more ads too:). 格式与我的常规索引不同,因此读者可以获得更大的照片,更多的文章文字(也包括更多广告:)。
In my routing table, I have the following statements: 在我的路由表中,我有以下语句:
resources :newsbites
get 'newsbites/frontindex'
root 'newsbites#frontindex'
Rake routes show the following: 耙路显示以下内容:
newsbites_frontindex GET /newsbites/frontindex(.:format) newsbites#frontindex
If I load my website from the root (localhost:3000), it works great. 如果我从根目录(localhost:3000)加载网站,则效果很好。 There is a separate menu page that is rendered at the top, and it loads fine.
顶部呈现了一个单独的菜单页面,并且可以正常加载。 I can click on all links, except the '
Home
' link, and they work fine. 我可以单击除“
Home
”链接以外的所有链接,它们可以正常工作。
The 'Home' link is: “主页”链接为:
<%= link_to 'Home', newsbites_frontindex_path %>
When I click on the linked, I get the following error: 当我单击链接时,出现以下错误:
Couldn't find Newsbite with 'id'=frontindex
The error points to the ' show
' action of my Newbites controller. 错误指向我的Newbites控制器的'
show
'操作。 Here are the frontindex
and show def from the controller. 这是
frontindex
并显示控制器的def。 They appear exactly as I'm posting them: 它们的显示方式与我发布它们的方式完全相同:
def frontindex
@newsbites = Newsbite.all
end
def show
@newsbite = Newsbite.find(params[:id])
end
I don't get it. 我不明白 Why is the show action being called by
newbites_frontindex_path
when there is both a def and views that match? 当同时具有def和视图时,为什么
newbites_frontindex_path
调用show动作? Now, I can get around this by simply pointing home to root_path
. 现在,我可以通过简单地指向
root_path
来解决此问题。 But that doesn't help me understand. 但这并不能帮助我理解。 What if this was not the root of the site?
如果这不是网站的根目录怎么办?
Any help would be greatly appreciated. 任何帮助将不胜感激。
Actually I'm very surprised your code worked at all. 实际上,我很惊讶您的代码完全奏效。 A route must define two things
一条路线必须定义两件事
newsbites/frontindex
is different than newsbites/backindex
) newsbites/frontindex
比不同newsbites/backindex
) Rails won't usually "guess" what that action is. Rails通常不会“猜测”该动作是什么。 Or maybe, he was still able to "guess" that you wanted to use the
newsbites
controller, but it didn't guess the action right this time :(. 或者,也许他仍然能够“猜测”您想使用
newsbites
控制器,但是这次没有猜测到正确的动作:(。
You should declare the root like this, which is what you did 您应该这样声明根,这就是您所做的
root 'controller#action'
For the rest, there are two ways you can declare it. 对于其余部分,有两种声明方法。 I prefer the second one
我喜欢第二个
resources :newsbites
get 'newsbites/frontindex', to: 'newsbites#frontindex'
resources :newsbites do
# stuff added here will have the context of the `newsbites` controller already
get 'frontindex', on: :collection # the name of the action is inferred to be `frontindex`
end
The on: :collection
, means that 'frontindex' is an action that concerns ALL the newsbites, so the URL generated will be newsbites/frontindex
. on: :collection
表示'frontindex'是涉及所有新闻比特的动作,因此生成的URL将是newsbites/frontindex
。
On the other hand get 'custom_action', on: :member
, means that the custom_action targets a specific item, the URL generated would look like newsbites/:id/custom_action
另一方面
get 'custom_action', on: :member
意味着该newsbites/:id/custom_action
以特定项目为目标,生成的URL看起来像newsbites/:id/custom_action
EDIT : Rails also generate path_helpers based on the route declaration 编辑 :Rails还基于路线声明生成path_helpers
get 'test', to: 'newsbites#frontindex'
get 'test/something', to: 'newsbites#frontindex'
resources :newsbites do
get 'frontindex', on: :collection
get 'custom_action', on: :member
Will generate path helpers 将生成路径助手
test_path
test_something_path
# CRUD helpers : new/edit/show/delete, etc. helpers
frontindex_newsbites_path
custom_actions_newsbite_path(ID) # without s !
You can always override this by adding an as:
option 您始终可以通过添加
as:
选项来覆盖它
get 'custom_action', on: :member, as: 'something_cool'
# => something_cool_newsbites_path
Rails routes thinks that frontindex
is an id. Rails路线认为
frontindex
是一个id。 That's what the error message says. 这就是错误消息的内容。 So it goes to
GET newsbite/:id
which maps to show
. 所以,去
GET newsbite/:id
映射到show
。
You need to find a way let Rails routes know that frontindex
is not an id
. 您需要找到一种使Rails路由知道
frontindex
不是id
。
On a side note: The order in which you define routes matters. 附带说明:定义路线的顺序很重要。 The first one matched will be used.
将使用第一个匹配的内容。 If you have
GET newsbite/:id
and GET newsbite/frontindex
then the one that appears first will be matched. 如果您具有
GET newsbite/:id
和GET newsbite/frontindex
,则将首先出现的那个匹配。 In your case this is the first one. 在您的情况下,这是第一个。 Maybe try to change the order.
也许尝试更改顺序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.