Rails button_to因存在的路径不存在而失败 [英] Rails button_to fails with path doesn't exist for a path that exists
问题描述
编写我的第一个非常简单的Rails应用程序,一个简单的管理应用程序来跟踪我们某个部门的工作。生成的人员索引页面上具有link_to以添加新人员。我尝试将其更改为button_to,但未能说明路径/ people / new不存在,尽管显然如此,因为link_to移至同一位置。
Writing my first, very simple Rails application, a simple admin app to track work for one of our departments. The generated index page for people has a link_to on it to add a new person. I tried to change that to button_to and it fails saying the path /people/new doesn't exist, though obviously it does since link_to goes to the same place.
我正在使用Rails 3 / Ruby 1.9.2。我在/app/views/people/index.html.erb页面上有以下代码:
I'm using Rails 3/Ruby 1.9.2. I have this code on my /app/views/people/index.html.erb page:
<%= link_to 'New Person', new_person_path %>
<%= button_to "New", :controller => "people", :action => "new" %>
link_to有效。 button_to失败,因为:
The link_to works. The button_to fails with this:
路由错误
没有路线匹配 / people / new
Routing Error No route matches "/people/new"
也尝试过
<%= button_to 'New Person', new_person_path %>
相同错误。
推荐答案
button_to
默认为post方法。尝试放入:method => :get
在那里。这就是为什么 link_to
起作用的原因。
button_to
defaults to the post method. Try putting :method => :get
in there. This is why link_to
works.
这篇关于Rails button_to因存在的路径不存在而失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!