[英]How to access URL in Rails Show action in Controller
This is the URL I want to access in my show method这是我想在我的显示方法中访问的 URL
http://localhost:3000/students.1 http://localhost:3000/students.1
ActiveRecord::RecordNotFound in StudentsController#show Couldn't find Student with 'id'= ActiveRecord::RecordNotFound in StudentsController#show 找不到具有 'id'= 的学生
def show定义显示
@student = Student.find(params[:id])
my routes are我的路线是
new_students GET /students/new(.:format) students#new new_students GET /students/new(.:format) 学生#new
edit_students GET /students/edit(.:format) students#edit 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
Can someone tell me how do I access that students.1 in my URL so that I can display that particular student on my show page?有人可以告诉我如何在我的 URL 中访问该 students.1 以便我可以在我的显示页面上显示该特定学生吗?
there is some issue with your route formation你的路线形成有问题
your url is not properly forming, try below你的 url 没有正确成型,试试下面
students_path(id: student.id)
Thats the wrong path.那是错误的道路。 It should be
/students/1
.它应该是
/students/1
。
And your routes are wrong.而且你的路线是错误的。 You can tell that the routes are off by the lack of an id segment and the fact that it maps
GET /students
to the show action instead of the index.您可以通过缺少 id 段以及将
GET /students
映射到 show 操作而不是索引这一事实来判断路由已关闭。
# Wrong
resource :students
# Right
resources :students
Pluralization is extremely important in Rails and is worth paying careful attention to.复数化在 Rails 中极为重要,值得认真关注。 While its just one tiny letter these methods produce completely different routes.
虽然只是一个小字母,但这些方法会产生完全不同的路线。
resource
is for singular resources . resource
用于奇异资源。 This is not the case here.这不是这里的情况。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.