簡體   English   中英

如何在 Rails 中訪問 URL 在 Controller 中顯示操作

[英]How to access URL in Rails Show action in Controller

這是我想在我的顯示方法中訪問的 URL

http://localhost:3000/students.1

但錯誤來了

ActiveRecord::RecordNotFound in StudentsController#show 找不到具有 'id'= 的學生

定義顯示

@student = Student.find(params[:id])

結尾

我的路線是

new_students GET /students/new(.:format) 學生#new

edit_students GET /students/edit(.:format) 學生#edit

 students GET    /students(.:format)      students#show

          PATCH  /students(.:format)      students#update

          PUT    /students(.:format)      students#update

          DELETE /students(.:format)      students#destroy

          POST   /students(.:format)      students#create

     root GET    /                        students#index

有人可以告訴我如何在我的 URL 中訪問該 students.1 以便我可以在我的顯示頁面上顯示該特定學生嗎?

你的路線形成有問題

你的 url 沒有正確成型,試試下面

students_path(id: student.id)

應該是http://localhost:3000/students/1

查看文檔

那是錯誤的道路。 它應該是/students/1

而且你的路線是錯誤的。 您可以通過缺少 id 段以及將GET /students映射到 show 操作而不是索引這一事實來判斷路由已關閉。

# Wrong
resource :students

# Right
resources :students

復數化在 Rails 中極為重要,值得認真關注。 雖然只是一個小字母,但這些方法會產生完全不同的路線。

resource用於奇異資源 這不是這里的情況。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM