Rails route to create, delete, update actions

I am trying to understand the routing of rails. I read the guidebook, but I'm still confused. For example, I have a post_controller with all rails crud actions, as shown below:

                    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

As you can see above, only actions index, new, edit and showhave a path name on the left. For example, indexaction has a path name posts, and I can get the url as posts_path. And I can use it in the link tag below

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

But there are no path names for creating, updating, and destroying actions. So, how can I get the url to create an action in this case for the link below?

<a href="<%= ..... link to create action of post controller  %>">here</a>      
+4
source share
4 answers

, , , :

<%= link_to posts_path(@post) %>

(@post = Post.new), , , , . link_to, method: :delete

+4

method link_to. , HTTP-:

<%= link_to "Update Post", post_path, method: :patch %>
+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' %>
+2
source

I recommend you these lectures, for me it helps me understand a lot. but basically you need to send the put method, patch, or delete routes in rails explain , patch and put to the 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%>

Do not forget that the identifier is more important, because your controller must know what post you need to complete to update or delete.

+1
source

All Articles