开发者

Rails button_to fails with path doesn't exist for a path that exists

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.

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" %&开发者_运维百科gt;

The link_to works. The button_to fails with this:

Routing Error No route matches "/people/new"

Also tried just

<%= button_to 'New Person', new_person_path %>

Same error. Odd.


button_to defaults to the post method. Try putting :method => :get in there. This is why link_to works.


There's a good explanation for this, as always :)

link_to uses GET as default, where button_to uses POST. And there's no POST route that matches, only a GET route.

If you want to use button_to, you can add :method => :get to your buttons params and it will use GET.


Did you set up your routing options in config/routes.rb? Check if you have this in your routes.rb file:

resources :people

Check this guide for more informations about how routes work.


Is your button_to inside a form? button_to creates a form of its own so this would create a form within a form and likely break routing.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜