最近我一直在使用Rails。我有一个带有form_tag的表单,该表单接收来自route.rb的帮助帖子。我在访问此表单时有相同的路由,在posrt中有路由,可以发送前一个表单的数据:
get 'students/:student_id/monitorings/inscribir', to: 'monitorings#inscribir', as: :inscribir_student_monitoring
post 'students/:student_id/monitorings/inscribir', to: 'monitorings#inscribir', as: :inscribir_student_monitoring_post
我的问题是,如何分隔这些路线?由于根据调查,这可能会引起问题,因为两者都有相同的地址,并且如果确实引起了我的麻烦,则因为在输入此链接时会自动执行表单。
您是否必须将两条路线分开?如果那是真的,我该怎么办? 我尝试过类似rails的方法,使该方法获得新方法和创建后方法,但适应了我的情况,但是对我没有用。
谢谢。
答案 0 :(得分:0)
您是否必须将两条路线分开?
不!除非您的两条路线都具有相同的名称,否则不会有问题。例如,如果您有以下路线
get 'students/:student_id/monitorings/inscribir', to: 'monitorings#inscribir', as: :inscribir_student_monitoring
post 'students/:student_id/monitorings/inscribir', to: 'monitorings#inscribir', as: :inscribir_student_monitoring
然后Rails会像这样跟踪异常
ArgumentError:无效的路由名称,已在使用中: 'inscribir_student_monitoring'您可能已经定义了两条路线, 使用
:as
选项使用相同的名称,否则您可能会覆盖路由 已经由具有相同命名的资源定义。对于后者, 您可以按照说明限制使用resources
创建的路由 这里: http://guides.rubyonrails.org/routing.html#restricting-the-routes-created
由于您的路线的名称不同,因此不必担心。尽管您可以使用match
match 'students/:student_id/monitorings/inscribir', to: 'monitorings#inscribir', via: [:get, :post]