简体   繁体   English

如何在 Rails 中访问 URL 在 Controller 中显示操作

[英]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

but the error is coming但错误来了

ActiveRecord::RecordNotFound in StudentsController#show Couldn't find Student with 'id'= ActiveRecord::RecordNotFound in StudentsController#show 找不到具有 'id'= 的学生

def show定义显示

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

end结尾

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)

It should be http://localhost:3000/students/1应该是http://localhost:3000/students/1

Check out this docs查看文档

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.

相关问题 如何显示一个控制器在导轨中的另一个控制器动作中显示动作 - how to display one controller show action in another controller action in rails 如何在Rails控制器中的show action中获取值 - How to get values in show action in rails controller 如何使用URL中的令牌限制对控制器的一个操作的访问 - Rails 3? - How do I restrict access to one action of a controller using a token in the URL - Rails 3? 在Rails控制器测试中,如何访问不同的控制器操作? - In a Rails controller test, how to access a different controller action? Rails 获取 controller 动作 url - Rails get controller action url Rails如何在控制器的show action中呈现两种格式的html / json - rails how to render two format html/json in show action of a controller 如何通过Rails中的url将参数发送到另一个控制器的动作? - how to send parameter to the action of another controller through url in rails? 如何从rails link_to中的URL中删除操作和控制器 - how to remove action and controller from url in rails link_to Rails:如何防止未经授权访问控制器更新操作 - Rails: How to prevent unauthorized access of controller update action 如何访问 Rails 5 Action Cable Channels 中的关注点和应用程序控制器代码 - How to access concerns and application controller code in Rails 5 Action Cable Channels
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM