Creating a new rails action doesn't work?

I have an Application controller. It consists of a single index action. Now I want to add a new action called "buy":

def buy respond_to do |format| format.html end end 

I added buy.html.erb to the views, but when I view / apps / buy, I get the following message:

 Unknown action - The action 'show' could not be found for AppsController 

in routes I added this:

  match '/apps/buy', :controller => 'apps', :action => 'buy' 

early!

+6
ruby-on-rails controller action routes
source share
1 answer

The url is captured by the standard route /apps/:id , I suppose you also have resources :apps in your routes?

Just put the purchase route first:

 match '/apps/buy', :controller => 'apps', :action => 'buy' resources :apps 

Keep in mind that routes run in the order in which they are defined, so specific ones must precede general ones.

A simpler approach that @Ryan offers is to add the collection route to the resource:

 resources :apps, :collection => { :buy => :get } 
+16
source share

All Articles