在我的Ruby应用中未能达成的新动作
我只是在轨道上学习Ruby,并构建一个相当简单的应用程序,该应用程序将信息存储到耙子生成的DB中。我正在尝试向控制器添加新操作,但我无法触发它。有一个fore_action
被触发,不应该是。
class GamesController < ApplicationController
before_action :set_entry, only: %i[ show edit update destroy ]
before_action :authenticate_user!
def index
end
def analyse
#The function here doesnt matter, its not reaching it because its hitting the set_entry instead
puts 'howdy'
end
private
def set_entry
@entry = Entry.find(params[:id])
end
end
我遇到的错误是无法使用'id'= Analyze找到游戏'
,突出显示了set_entry
操作,对我的理解只能使用<<代码> show ,<代码>编辑,更新 destion action(也在完整的控制器中,运行良好)是控制器中的其他操作(例如创建
和new
),似乎不会触发set_entry
,并且按预期运行良好例如。 /entry/:id/edit ,这一切都很好。它要转到entry/
analyze
<%= link_to "Analyse", analyse_path %>
。
Rails.application.routes.draw do
resources :entry
devise_for :users
resources :users
root to: "entry#index"
get 'entry/index'
get 'entry/analyse', as: 'analyse'
end
I'm just learning Ruby on Rails and building a fairly simple app that stores info to a rake generated db. I'm trying to add a new action to the controller but I can't trigger it. There's a before_action
getting triggered which shouldn't be.
class GamesController < ApplicationController
before_action :set_entry, only: %i[ show edit update destroy ]
before_action :authenticate_user!
def index
end
def analyse
#The function here doesnt matter, its not reaching it because its hitting the set_entry instead
puts 'howdy'
end
private
def set_entry
@entry = Entry.find(params[:id])
end
end
The error I'm hitting is Couldn't find Game with 'id'=analyse"
, highlighting the set_entry
action, which to my understanding should only be running with the show
, edit
, update
, and destroy
actions (which are also in the full controller and running fine). There are other actions in the controller (such as create
and new
) which don't seem to trigger that set_entry
and are running just fine as expected. For example, linking to the new
path takes me to /entry/new
, while linking to an edit
path takes me to /entry/:id/edit
, which is all fine. But it keeps linking my new analyse
action trying for entry/:id/analyse
when I want it to go to entry/analyse
.
My button to trigger it is simply:
<%= link_to "Analyse", analyse_path %>
Which is in a navbar in my application.html.erb
And here's my routes.rb:
Rails.application.routes.draw do
resources :entry
devise_for :users
resources :users
root to: "entry#index"
get 'entry/index'
get 'entry/analyse', as: 'analyse'
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
路径
/entry/Analyze
匹配两个路由:entry#show
in资源中的操作:entry
条目#分析
中的操作获取'entry/Analyze',as:'Analyze'
,因为路由匹配是通过
config/doutes.rb
中的 code>资源解释的。 是匹配路径的第一个路线。在结果中,输入#show
操作将从/entry/Analyze
中处理请求。解决方案很简单,只需切换
资源的顺序:entry
和获取'entry/Analyze',as:'Analysze'
inconfig> config/doutes.rb < /代码>。例如:
The path
/entry/analyse
is matched two routes:entry#show
action inresources :entry
entry#analyse
action inget 'entry/analyse', as: 'analyse'
Because the route matching is interpreted through
config/routes.rb
in order, andresources :entry
is the first route the path is matched. At the result,entry#show
action will handle the requests from/entry/analyse
.The solution is simple, just switch the order of
resources :entry
andget 'entry/analyse', as: 'analyse'
inconfig/routes.rb
. For example:将所有
Resources
移动到路由配置的尽头。我不确定为什么它被绊倒了,但是您的路线中似乎是将分析与您的条目Resource
匹配,并且轨道内的路由是按顺序匹配的。所有自定义路线确实应该首先出现,以防止像通用路线一样捕捉您的预期动作。最佳实践还可以将您的
root表示为:
应该位于配置的顶部,因为它通常是您在应用程序中最受欢迎的路线。Move all of your
resources
to the end of your route configuration. I am not sure why its getting tripped up but it seems something within your routes is matching analyse to your entryresource
and routes within rails are matched in order. All custom routes really should come first to prevent something like a generic route catching your expected action.Best practices also state your
root to:
should be at the top of the configuration since it is your most popular route generally in an application.