2013-08-22 49 views
0

我正在嘗試爲Rails應用程序創建一系列靜態頁面。 「關於」頁面正常工作,但當我嘗試使用「條款」頁面的相同方法時,我得到一個未知的操作。我認爲這是與我的控制器。爲什麼我會收到「未知的行爲行爲'show'找不到」?

這是我的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 

任何想法是怎麼回事?

回答

2

你誤解了參數的意義,它的目的是自定義命名路線。 通過文檔 ActionDispatch::Routing Rails按照從上到下的順序匹配路由,所以這是您看到的行爲。

提取術語之間和術語之間的共同邏輯,以及術語指向他們自己的控制器動作。

-1

因爲你沒有創建行動showPagesController

def show 
end 

:as => 'about'在你的路由意味着你甚至可以從像about_pathabout_url代碼這個幫手,但它仍然需要:action => 'show'

+0

如果我只有路線:'match'/ about'=>'pages#about''和'match'/ show'=>'pages#show'' 則顯示'localhost:3000/about' about view,'localhost:3000/show'顯示show view。但是如果我在前兩條路由後添加路由:'match':permalink',:controller =>'pages',:action =>'show',:as =>'about'',那麼'localhost:3000/show'產生一個路由錯誤:'沒有路由匹配{:controller =>「pages」,:action =>「show」}'爲什麼?耙路由: about /about(.:format)pages#about show /show(.:format)pages#show about /:permalink(.:format)pages#show – 7stud

+0

對於同一個動作,你有兩條路由: 'match'/ show'=>'pages#show'' and'match':permalink',:controller =>'pages',:action =>'show'' – zolter

6

我的情況下,與以上情況不同,所以我爲了幫助而寫作。 我看到了同樣的錯誤,我明白,錯誤是由於一些路由問題,而這裏是我的路線

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

後來我改變

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

我的路線工作的安排 - 快樂編碼:)

相關問題