เส้นทาง Rails สำหรับการสร้าง ลบ อัปเดตการดำเนินการ

ฉันกำลังพยายามทำความเข้าใจการกำหนดเส้นทางราง ฉันได้อ่านคู่มือ Rails แล้ว แต่ฉันยังคงสับสนอยู่ ตัวอย่างเช่น ฉันมี post_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 และฉันสามารถใช้มันในแท็กลิงค์ได้ดังนี้

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

แต่ไม่มีชื่อเส้นทางสำหรับการสร้าง อัปเดต และทำลายการดำเนินการ ดังนั้นฉันจะรับ url สำหรับสร้างการกระทำในกรณีนี้สำหรับลิงค์ด้านล่างได้อย่างไร

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

person Community    schedule 11.04.2016    source แหล่งที่มา


คำตอบ (4)


ส่งเส้นทางและรหัสของโพสต์ที่คุณต้องการลบหรือวัตถุที่คุณต้องการสร้าง:

<%= link_to posts_path(@post) %>

หากคุณอยู่ในแบบฟอร์มและมีวัตถุ (@post = Post.new) รางจะทราบเมื่อส่งว่าคุณต้องการสร้างตามข้อเท็จจริงที่ว่าคุณใช้เส้นทางนั้นเพื่อส่งแบบฟอร์ม หากคุณต้องการลบโดยใช้ link_to คุณจะต้องผ่าน method: :delete

person toddmetheny    schedule 11.04.2016

จริงๆ แล้วมี _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 เป็นค่าเริ่มต้นสำหรับลิงก์ที่เข้าถึง) แต่คุณยังสามารถใช้ตัวช่วย _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' %>
person Zubatman    schedule 11.04.2016


ฉันขอแนะนำให้คุณฟังการบรรยายนี้สำหรับฉันช่วยให้ฉันเข้าใจสิ่งนี้ได้มาก แต่โดยพื้นฐานแล้วคุณต้องส่งเมธอด put, patch หรือ Delete อธิบายเส้นทางใน 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%>

อย่าลืมรหัสนั้นสำคัญเพราะตัวควบคุมของคุณจำเป็นต้องรู้ว่าโพสต์ใดจำเป็นต้องทำการอัปเดตหรือลบ

person Ezequiel García    schedule 11.04.2016