【问题标题】:How do I get my link_to to go to my show method?如何让我的 link_to 转到我的 show 方法?
【发布时间】:2016-12-07 22:36:38
【问题描述】:

我正在使用 Rails 5。我无法链接到控制器中的 show 方法。我在我的配置/路由中定义了这个

  resources :scenarios do
    resources :confidential_memos
  end

在一个视图中,我把这个

<td><%= link_to "#{subscription.scenario.title}", scenarios_path(subscription.scenario) %>

在我的控制器中我有 show 方法

class ScenariosController < ApplicationController

  def show
    @scenario = Scenario.find(params[:id])
  end

但是当我点击链接时,我得到了错误

The action 'index' could not be found for ScenariosController

如何获取我的链接以访问我的表演操作?

【问题讨论】:

  • 尝试scenario_path(单数)

标签: ruby-on-rails show ruby-on-rails-5 rails-routing link-to


【解决方案1】:

scenarios_path 使用复数“场景”,因此它正在寻找几个场景并映射到 index 操作,因此您的错误。要显示单个场景,您可以使用单数:

scenario_path(subscription.scenario)

如果您只有 show 操作,那么您可能希望在您的路线中明确:

resources :scenarios, :only => %i[ show ] do
  #...
end

以便您更快地发现错误。

您可以通过rake routes 自己解决此问题并查找您感兴趣的表演动作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-21
    • 2020-12-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多