Rails 创建、删除、更新操作的路由

Rails route for create, delete, update action

我正在尝试了解 rails 路由。我已阅读 rails 指南,但我仍然感到困惑。例如,我有一个 posts_controller,其中包含所有 rails crud 操作,如下所示:

                    posts GET    /posts(.:format)                     posts#index
                          POST   /posts(.:format)                     posts#create
                 new_post GET    /posts/new(.:format)                 posts#new
                edit_post GET    /posts/:id/edit(.:format)            posts#edit
                     post GET    /posts/:id(.:format)                 posts#show
                          PATCH  /posts/:id(.:format)                 posts#update
                          PUT    /posts/:id(.:format)                 posts#update
                          DELETE /posts/:id(.:format)                 posts#destroy

正如我从上面看到的,只有 index, new, edit and show 操作在左侧有路径名。例如,index 操作有一个路径名称 posts,我可以将 url 作为 posts_path。我可以在 link 标签中使用它,如下所示

<a href="<%= posts_path %>">here</a>

但是没有用于创建、更新和销毁操作的路径名。那么在这种情况下,如何为下面的 link 获取 url 以创建操作?

<a href="<%= ..... link to create action of post controller  %>">here</a>      

传递路径和您要删除的 post 或您要创建的对象的 ID:

<%= link_to posts_path(@post) %>

如果您在一个表单中,并且有一个对象 (@post = Post.new) rails 将根据以下事实知道您要创建的提交您正在使用该路线提交表格。如果要使用 link_to 删除,则需要传递 method: :delete

您需要在 link_to method 中使用 method 属性。路由名称相同,只是 HTTP 动词不同:

<%= link_to "Update Post", post_path, method: :patch %>

所以实际上所有生成的路由都有_path helpers,我在下面生成的路由前面添加了路径名,稍后我会解释它们的区别:

                posts GET    /posts(.:format)                     posts#index
                posts POST   /posts(.:format)                     posts#create
             new_post GET    /posts/new(.:format)                 posts#new
            edit_post GET    /posts/:id/edit(.:format)            posts#edit
                 post GET    /posts/:id(.:format)                 posts#show
                 post PATCH  /posts/:id(.:format)                 posts#update
                 post PUT    /posts/:id(.:format)                 posts#update
                 post DELETE /posts/:id(.:format)                 posts#destroy

因此,您向服务器发出的任何 GET 请求都可以简单地使用给定路径完成(因为 GET 是任何访问的默认路径 link),但您仍然可以使用 _path 帮助程序访问其他路径通过显式说明您用于访问的方法来路由。例如:

Index:
   <%= link_to "Index", posts_path %>

Create:
   <%= link_to "Create", posts_path, method: 'POST' %>

New:
   <%= link_to "New", new_post_path %>

Edit:
   <%= link_to "Edit", edit_post_path(post_id) %>

Show:
   <%= link_to "Show", post_path(post_id) %>

Update:
   <%= link_to "Update", post_path(post_id), method: 'POST' %>
   <%= link_to "Update", post_path(post_id), method: 'PATCH' %>

Destroy:
   <%= link_to "Destroy", post_path(post_id), method: 'DELETE' %>

我向你推荐这个讲座,对我来说帮助我理解这一点。但基本上您需要发送方法 put、patch 或 delete Routes in rails explain , patch and put for rails

<%= link_to "Update Post", posts_path(@post.id), method: :patch %>
<%= link_to "Update Post", posts_path(@post.id), method: :put %>
<%= link_to "delete Post", posts_path(@post.id), method: :delete%>

不要忘记 id 它很重要,因为您的控制器需要知道 post 需要什么来进行更新或删除操作。