问题描述
我有一个控制器Apps。它由一个动作索引组成。现在我想添加一个名为购买的新操作:
def buy
respond_to do | format |
format.html
end
end
.html.erb到视图,但是当浏览到/ apps / buy时,我会收到以下消息:
未知操作 - 在我添加此路线的路线中找不到AppsController
match'/ apps / buy',:controller => 'apps',:action => 'buy'
提前感谢!
这个网址被标准的 / apps /:id
路由捕获,我假设你也有
<$> p $ p> match'/ apps / buy',:controller => 'apps',:action => 'buy'
资源:apps
请记住,路由按顺序执行
一个更简单的方法是@Ryan建议在资源中添加一个集合路由:
资源:apps,:collection => {:buy => :get}
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 }
这篇关于创建新rails操作不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!