【问题标题】:Why do I get "Unknown action The action 'show' could not be found"?为什么我会收到“未知动作无法找到动作‘显示’”?
【发布时间】:2013-08-24 14:56:37
【问题描述】:

我正在尝试为 Rails 应用程序创建一系列静态页面。 “关于”页面工作正常,但是当我尝试对“条款”页面使用相同的方法时,我得到了一个未知的操作。我假设这是我的控制器。

这是我的 routes.rb 文件:

resources :pages
get "pages/index"

match '/about' => 'pages#about'
match ':permalink', :controller => 'pages', :action => 'show', :as => 'about'

match '/terms' => 'pages#terms'
match ':permalink', :controller => 'pages', :action => 'show', :as => 'terms'


root :to => 'pages#index'

我的控制器如下所示:

class PagesController < ApplicationController
  helper_method :search_performed?
  def index
    @search = Farm.search(params[:q])
    @json = @search.result.to_gmaps4rails 
  end

  protected
  def search_performed?
    params[:q].present?
  end

  def about
  end

  def feedback
  end

  def terms
  end

end

知道发生了什么吗?

【问题讨论】:

    标签: ruby-on-rails ruby static controller routes


    【解决方案1】:

    您误解了该参数的用途,它旨在自定义命名路由。 通过文档 ActionDispatch::Routing Rails 按从上到下的顺序匹配路线,所以这是您看到的行为。

    提取term和about之间的共同逻辑,让about和term指向它们自己的控制器动作。

    【讨论】:

      【解决方案2】:

      因为您没有在PagesController 中创建操作show

      def show
      end
      

      :as =&gt; 'about' 在你的路由中意味着你可以从about_pathabout_url 之类的代码中调用这个助手,但它仍然需要:action =&gt; 'show'

      【讨论】:

      • 如果我只有路线:match '/about' =&gt; 'pages#about'match '/show' =&gt; 'pages#show' 然后localhost:3000/about 显示about 视图,localhost:3000/show 显示show 视图。但是如果我在前两条路由之后添加路由:match ':permalink', :controller =&gt; 'pages', :action =&gt; 'show', :as =&gt; 'about',那么localhost:3000/show 会产生路由错误:No route matches {:controller=&gt;"pages", :action=&gt;"show"} 为什么会这样? rake 路由:关于 /about(.:format) pages#about 显示 /show(.:format) pages#show about /:permalink(.:format) pages#show
      • 同一操作有两条路线:match '/show' =&gt; 'pages#show'match ':permalink', :controller =&gt; 'pages', :action =&gt; 'show'
      【解决方案3】:

      我的情况与上述情况不同,所以我写信是为了帮助。 我查看了同样的错误,我知道错误是由于某些路由问题造成的,而这是我的路线

      resources 'customers'
        get "/customers/dashboard" => "customers#dashboard"
      

      然后我改变了安排

        get "/customers/dashboard" => "customers#dashboard"
      resources 'customers'
      

      我的路线成功了 - 编码愉快 :)

      【讨论】:

        猜你喜欢
        • 2018-08-08
        • 2013-01-05
        • 1970-01-01
        • 1970-01-01
        • 2021-09-21
        • 2015-08-02
        • 2015-01-03
        • 1970-01-01
        • 2013-09-08
        相关资源
        最近更新 更多