link_to :action => 'create' going to index rather than 'create'

◇◆丶佛笑我妖孽 提交于 2019-11-30 04:49:22

In the standard REST scheme the index action and the create action both have the same url (/recipes) and only differ in that index is accessed using GET and create is accessed using POST. So link_to :action => :create will simply generate a link to /recipes which will cause the browser to perform a GET request for /recipes when clicked and thus invoke the index action.

To invoke the create action use link_to {:action => :create}, :method => :post, telling link_to explicitly that you want a post request, or use a form with a submit button rather than a link.

Assuming you have default resources set up in your routes file, i.e. something like this

resources :recipes

The following will generate a link that will create a recipe; i.e. will be routed to the create action.

<%= link_to "Create Recipe", recipes_path, :method => :post %>

For this to work, JS needs to be enabled in your browser.

The following will generate a link that will show all recipes; i.e. will be routed to the index action.

<%= link_to "All Recipes", recipes_path %>

This assumes the default which is a Get HTTP request.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!