简体   繁体   中英

Creating new rails action doesn't work?

i have a controller "Apps". It consists of one action "index". Now I want to add a new action called "buy":

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

i added a buy.html.erb to the views, but when browsing to /apps/buy, i get following message:

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

in the routes I added this:

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

thanks in advance!

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

Simply place the buy route first:

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

Bear in mind that routes are executed in the order they are defined, so the specific ones need to precede the general.

A simpler approach as @Ryan suggests is adding a collection route to the resource:

resources :apps, :collection => { :buy => :get }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM