Reputation:
I am trying to understand rails routing. I have read rails guide but I am still in confusion. For example, I have a posts_controller with all rails crud actions like 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 I can see from above only index, new, edit and show
actions have a path name on the left. For example, index
action has a path name posts
and I can get the url as posts_path
. And I can use it in link tag as below
<a href="<%= posts_path %>">here</a>
But there is no path names for create, update and destroy actions. So how can I get the url for create action in this case for the link below?
<a href="<%= ..... link to create action of post controller %>">here</a>
Upvotes: 4
Views: 5255
Reputation: 2776
i recommend to you this lectures, for me helps me a lot for understand this. but Basically you need to send the method put, patch or 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%>
dont forget the id its important because your controller need to know what post need to make the update or delete action.
Upvotes: 1
Reputation: 1305
So there are actually _path helpers on all of the generated routes, I have added the path names in front of the generated routes below, I'll explain the difference in just a moment:
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
So any GET request you make to the server can simply be done with the given path (since GET is the default for any accessed link), but you can still use the _path helper to access the other routes by explicitly stating the method that you are using to access with. For example:
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' %>
Upvotes: 5
Reputation: 16512
You need to use the method
attribute in the link_to
method. The route name is the same, but just with different HTTP verbs:
<%= link_to "Update Post", post_path, method: :patch %>
Upvotes: 2
Reputation: 4453
Pass the path and either an id of the post you'd like to delete or the object you'd like to create:
<%= link_to posts_path(@post) %>
If you're inside a form, and have an object (@post = Post.new) rails will know on submission that you want to create based on the fact that you're using that route to submit a form. If you want to delete using a link_to, you'll need to pass method: :delete
Upvotes: 4