【问题标题】:Routes not working on first visit首次访问时无法使用的路线
【发布时间】:2013-02-08 03:37:36
【问题描述】:

我有一个命名空间,我刚刚为特定模型的路由文件添加了编辑和更新。我的问题是,当我从应用程序中的 link_to 访问编辑路由时,它说没有找到路由,但是,如果我手动输入相同的 url,它会找到它并正确呈现页面。我要访问的网址是coordinator/teacher_recommendation/?/edit 显示效果很好。

这是我的路线文件的相关部分:

 namespace :coordinator do
   resources :applicants, :only => [:show, :edit, :update, :index] do
     member do
       get 'invite_teachers'
       put 'create_invitations'
     end
   end
   resources :teacher_recommendations, :only => [:show, :edit, :update] do
     resources :recommendation_reminders, :only => [:create, :new, :update]
   end  
 end 

关于为什么会这样的任何想法?

这是不起作用的 button_to 链接:

button_to "Send Reminder", edit_coordinator_teacher_recommendation_path(@applicant.science_recommendation)

这里是可以工作的link_to:

link_to "Send Reminder", edit_coordinator_teacher_recommendation_path(@applicant.science_recommendation)

【问题讨论】:

  • 您能否运行 firebug 或 Chrome 开发者工具来查看在这两种情况下请求的确切 URL?也许他们有一些微妙的不同。另外,如果你手动访问链接后使用link_to,是否有效?
  • 运行 rake routes 并将其粘贴到此处。另外,请粘贴您的<%= link_to EDIT link_path_goes_here %>
  • 发布您正在使用的link_to 方法的代码。
  • 完全相同的URL,一旦进入该URL并报告没有找到路由,我可以将光标放在地址栏中并按Enter键,它会正常呈现页面。不,手动访问页面后也不能使用 link_to 。此外,如果我使用刷新并确认重新提交,它将不起作用。看来应该是帖子的问题了……
  • 好的.. 更正。该链接是“button_to”链接。当我将其更改为 link_to 时,页面会正确呈现。 link_to 和 button_to 有什么区别?

标签: ruby-on-rails routes


【解决方案1】:

我将 button_to 链接更改为具有方法参数,这似乎解决了问题...

button_to "发送提醒", edit_coordinator_teacher_recommendation_path(@applicant.science_recommendation), class: "btn btn-mini", :method => :get %>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-23
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 2019-12-14
    相关资源
    最近更新 更多