简体   繁体   English

创建新的rails操作不起作用?

[英]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": 现在我想添加一个名为“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: 我在视图中添加了buy.html.erb,但在浏览/ apps / buy时,我收到以下消息:

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? 该网址被标准/apps/:id路由捕获,我假设您还拥有resources :apps路由中的resources :apps

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: @Ryan建议的一种更简单的方法是向资源添加一个收集路由:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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