【问题标题】:Rails route for create, delete, update action用于创建、删除、更新操作的 Rails 路由
【发布时间】:2016-04-11 19:19:09
【问题描述】:

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

                    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,我可以得到posts_path 的url。我可以在下面的链接标签中使用它

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

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

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

【问题讨论】:

    标签: ruby-on-rails url-routing


    【解决方案1】:

    传递路径和您要删除的帖子的 id 或您要创建的对象:

    <%= link_to posts_path(@post) %>
    

    如果您在表单中,并且有一个对象 (@post = Post.new),rails 将根据您使用该路由提交表单的事实在提交时知道您要创建的对象。如果你想使用链接删除,你需要传递method: :delete

    【讨论】:

      【解决方案2】:

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

                      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 是任何访问链接的默认值),但您仍然可以使用 _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' %>
      

      【讨论】:

        【解决方案3】:

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

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

        【讨论】:

          【解决方案4】:

          我向您推荐这个讲座,因为我对理解这一点有很大帮助。但基本上你需要发送方法 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 它很重要,因为您的控制器需要知道需要执行更新或删除操作的帖子。

          【讨论】:

            猜你喜欢
            • 2023-03-23
            • 1970-01-01
            • 1970-01-01
            • 2011-09-25
            • 1970-01-01
            • 2021-06-20
            • 1970-01-01
            • 1970-01-01
            • 2011-11-05
            相关资源
            最近更新 更多