为什么我会收到“未知动作无法找到动作'节目'”?

时间:2021-04-09 19:44:15

I am trying to create a series of static pages for a Rails app. The "about" page works fine but when I try using the same method for the "terms" page I get an unknown action. I am assuming this is with my controller.

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

This is my routes.rb file:

这是我的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'

My controller looks like this:

我的控制器看起来像这样:

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

Any idea what's going on?

知道发生了什么事吗?

3 个解决方案

#1


3  

You are misunderstanding what the parameter as is for, its intended to customize named routes. via the doc ActionDispatch::Routing Rails matches routes in order from top to bottom, so this is the behavior you are seeing.

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

Extract the common logic between terms and about and have about and terms point to their own controller actions.

提取术语和约之间的共同逻辑,并且有关于和术语指向他们自己的控制器动作。

#2


7  

My case was different from above cases , so i'm writing for the sake of help . I viewed the same error and i understand that error was due to some routing problems , while here was my route

我的情况与上述情况不同,所以我写的是为了帮助。我查看了相同的错误,我理解错误是由于一些路由问题,而这是我的路线

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

Then i changed the arrangement

然后我改变了安排

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

My routes worked - Happy coding :)

我的路线工作 - 快乐编码:)

#3


-1  

Because you did not create action show in your PagesController

因为您没有在PagesController中创建动作节目

def show
end

:as => 'about' in your routes means that you can call this helper from code like about_path or about_url, but it still need :action => 'show'

:as =>你的路线中的'about'意味着你可以从像about_path或about_url这样的代码调用这个帮助器,但它仍然需要:action =>'show'

#1


3  

You are misunderstanding what the parameter as is for, its intended to customize named routes. via the doc ActionDispatch::Routing Rails matches routes in order from top to bottom, so this is the behavior you are seeing.

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

Extract the common logic between terms and about and have about and terms point to their own controller actions.

提取术语和约之间的共同逻辑,并且有关于和术语指向他们自己的控制器动作。

#2


7  

My case was different from above cases , so i'm writing for the sake of help . I viewed the same error and i understand that error was due to some routing problems , while here was my route

我的情况与上述情况不同,所以我写的是为了帮助。我查看了相同的错误,我理解错误是由于一些路由问题,而这是我的路线

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

Then i changed the arrangement

然后我改变了安排

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

My routes worked - Happy coding :)

我的路线工作 - 快乐编码:)

#3


-1  

Because you did not create action show in your PagesController

因为您没有在PagesController中创建动作节目

def show
end

:as => 'about' in your routes means that you can call this helper from code like about_path or about_url, but it still need :action => 'show'

:as =>你的路线中的'about'意味着你可以从像about_path或about_url这样的代码调用这个帮助器,但它仍然需要:action =>'show'