jdwallace12
jdwallace12

Reputation: 127

Why do I get "Unknown action The action 'show' could not be found"?

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.

This is my routes.rb file:

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?

Upvotes: 0

Views: 10627

Answers (3)

Mani
Mani

Reputation: 2553

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 :)

Upvotes: 8

zolter
zolter

Reputation: 7160

Because you did not create action show in your 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'

Upvotes: -1

TheIrishGuy
TheIrishGuy

Reputation: 2573

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.

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

Upvotes: 3

Related Questions