views:

66

answers:

3
+2  Q: 

Action not found

Hi people. 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?

+4  A: 

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

map.connect 'mycontroller/calculator', :controller => 'mycontroller', :action => 'calculator'
Trevoke
That works great. I figured out that the line must be **before** map.resources line, when common sense told me it should be after. Thanks!
benoror
+1  A: 

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.

Nils Riedemann
A: 

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

thorncp