Ale*_*ton 1 routing ruby-on-rails
是什么之间的差异:
http://localhost:3000/courses/edit.2
and
http://localhost:3000/courses/edit?id=2
Run Code Online (Sandbox Code Playgroud)
它由<%= edit_courses_path(course)%>生成
和link_to({action :: edit,id:course.id})分别
http://localhost:3000/courses/edit.2 # This link does not work
http://localhost:3000/courses/edit?id=2 # This link works
Run Code Online (Sandbox Code Playgroud)
我该怎么做才能使这两个链接都有效?
也不是Rails 资源丰富的路由.通过以下方式实现资源丰富的编辑路线:
resources :courses
Run Code Online (Sandbox Code Playgroud)
然后您可以使用自动生成的路径助手(查看rake routes)来创建链接:
<%= link_to "Edit course", edit_course_path(course) %>
Run Code Online (Sandbox Code Playgroud)
/courses/2/edit例如,生成一个链接,params[:id]设置为2.注意edit_course_path与您相比的单数edit_courses_path.
你的版本产生的原因/courses/edit.2是因为没有动态段的路由助手将第一个参数作为格式,所以你告诉它格式是"2"(而不是xml,json,pdf等).