【问题标题】:Having problem with routes.rb. No route matches show actionroutes.rb 有问题。没有路线匹配显示操作
【发布时间】:2020-05-29 11:07:04
【问题描述】:

我正在尝试制作一个按钮。当您单击它时,应该会出现一个带有表单的模式。

我创建了按钮:

<li>
<%= link_to content_tag(:i, nil, class: "fa fa-plus") + " New Rule",new_rule_correlation_engine_rule_path, class: "pull-right panel-button", 'data-toggle' => "modal", 'data-target' => "#new_rule_correlation_engine_rule_modal", "data-backdrop" => "static" %>
</li>

routes.rb 中,我定义了第一条路线(资源部分已经存在):

get 'correlation_engine_rules/new_rule' => 'correlation_engine_rules#new_rule', as: 'new_rule_correlation_engine_rule'

    resources :correlation_engine_rules do
      post :apply, on: :collection
    end

我还创建了一个名为 new_rule 的文件,在开头有一个记录器来了解是否正在加载它。显然,当我单击按钮时,它会加载该文件,因为我看到了记录器,但随后出现此错误:

<ActionController::UrlGenerationError: No route matches {:action=>"show", :controller=>"correlation_engine_rules"} missing required keys: [:id]>

所以,路线与我的第一行匹配,但似乎它也在尝试执行方法 show,我猜是因为我下面的资源行。起初我的路线低于该部分,然后我看到了这个错误,我把它放在前面,如路由导轨指南中所述,但我仍然收到这个错误。我认为当 1 条路线匹配时,它会停止寻找更多,也许我错了,但我不明白问题出在哪里。谢谢。

解决方案:我终于通过在资源块中输入新路线解决了这个问题,如下所示:

resources :correlation_engine_rules do
      post :apply, on: :collection
      get :new_rule, on: :collection
end

然后我将其称为 new_rule_correlation_engine_rules_path。

【问题讨论】:

  • 为什么你认为你应该首先复制现有路线/新行动并打破惯例?你已经有一个new_correlation_engine_rule_path 路由 - 使用它。
  • 我已经有 1 个按钮可以调用您正在冷藏的新方法。问题是我想做一个完全不同的事情,并且渲染一个不同的视图,所以我虽然需要做这个。正如您所说,已经制作的按钮调用了 new_correlation_engine _rule_path,它呈现了一个名为“new”的文件。但是现在我想渲染一个名为 new_rule 的文件,这就是为什么我要这样做。我还在学习 Rails,所以这可能是非常基础的。

标签: ruby-on-rails routes


【解决方案1】:

你试过指定http方法GET吗?,类似这样的

<%= link_to content_tag(:i, nil, class: "fa fa-plus") + " New Rule",new_rule_correlation_engine_rule_path, method: :get, class: "pull-right panel-button", 'data-toggle' => "modal", 'data-target' => "#new_rule_correlation_engine_rule_modal", "data-backdrop" => "static" %>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-08
    • 2020-12-01
    • 2012-05-09
    相关资源
    最近更新 更多