在我的Ruby应用中未能达成的新动作

发布于 2025-01-27 10:21:03 字数 1265 浏览 1 评论 0原文

我只是在轨道上学习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 技术交流群。

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

发布评论

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

评论(2

空城仅有旧梦在 2025-02-03 10:21:03

路径/entry/Analyze匹配两个路由:

  1. entry#show in 资源中的操作:entry
  2. 条目#分析 中的操作获取'entry/Analyze',as:'Analyze'

,因为路由匹配是通过config/doutes.rb中的 code>资源解释的。 是匹配路径的第一个路线。在结果中,输入#show操作将从/entry/Analyze中处理请求。

解决方案很简单,只需切换资源的顺序:entry获取'entry/Analyze',as:'Analysze' in config> config/doutes.rb < /代码>。例如:

Rails.application.routes.draw do
  get 'entry/analyse', as: 'analyse' # <- to here
  resources :entry
  devise_for :users
  resources :users
  root to: "entry#index"
  get 'entry/index'
  # from here ...
end

The path /entry/analyse is matched two routes:

  1. entry#show action in resources :entry
  2. entry#analyse action in get 'entry/analyse', as: 'analyse'

Because the route matching is interpreted through config/routes.rb in order, and resources :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 and get 'entry/analyse', as: 'analyse' in config/routes.rb. For example:

Rails.application.routes.draw do
  get 'entry/analyse', as: 'analyse' # <- to here
  resources :entry
  devise_for :users
  resources :users
  root to: "entry#index"
  get 'entry/index'
  # from here ...
end
柳絮泡泡 2025-02-03 10:21:03

将所有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 entry resource 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.

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