Ben Orozco
Ben Orozco

Reputation: 4371

Action not found

I want to show a simple HTML template, so I added a new empty action to my controller:

def calulator
end

And created the view calculator.html.erb . Then added a link to it:

<%= link_to 'Calculator', {:controller => "mycontroller", :action => "calculator"} %>

When I click it my log shows the following error:

ActionController::UnknownAction (No action responded to show. Actions: calculator, create, destroy, edit, index, new, and update):

Why is looking for a "show" action ? I have map.resources for the controller, as I done it with scaffolding

Any ideas?

Upvotes: 4

Views: 2029

Answers (3)

thorncp
thorncp

Reputation: 3627

You are getting No action responded to show because you have the controller routed as map.resources. When you do that, rails sets up several routes for you. One of which is show, which maps every get request matching /mycontroller/somevalue to the show action with somevalue as the id (params[:id]). In mycontroller, there is no show action, as seen in the error message.

To fix this, Nils or Trevoke's answer should work just fine.

map.resources documentation

Upvotes: 0

nocksock
nocksock

Reputation: 5527

You can define members and collections for resources.

map.resources :samples, :member => {:calculator => :get}

Member means that it relates to an instance of the resources. For example /samples/1/calculator. If it doesn't relate to an instance you can define it for the collection and can be access via /samples/calculator.

map.resources :samples, :collection => {:calculator => :get}

This also creates a helper method calculator_samples_path for the collection and calculator_sample_path(sample) for a member. For more on this have a look at Railscast Episode 35.

Upvotes: 1

Trevoke
Trevoke

Reputation: 4117

You need to add a custom route pointing to the action 'calculator'. Something like this:

map.connect 'mycontroller/calculator', :controller => 'mycontroller', :action => 'calculator'

Upvotes: 5

Related Questions