【问题标题】:Rails 4: Routing with partialsRails 4:使用局部路由
【发布时间】:2016-05-13 18:01:36
【问题描述】:

在我的 Rails 应用程序中导航时出现以下错误: 没有路线匹配

{:action=>"startnew", :controller=>"tickets", :ticket_id=>4} missing required keys: [:id]

问题是,我有一条路线:

  post   'tickets/:id/startnew' => 'tickets#startnew', as: :start_new

当我导航到 URL http://localhost:3000/tickets 时,这很好用 但是当我加载 URL http://localhost:3000/tickets?id=4 时会发生错误,在我的情况下它需要工作,因为我在tickets#index 上的两个部分中呈现了tickets#list 和tickets#show,其中发生了上述错误。 当我单击不同页面上的链接时,tickets#index 被加载,并且仅当我在 url (http://localhost:3000/tickets?id=4) 中有参数时才会呈现tickets#show。

在节目中我有这部分,这会导致问题:

<%= link_to 'Book time', start_new_path(ticket_id: @ticket.id), method: :post, class: "button info block-shadow-info text-shadow", remote: true %>

你能不能给我一个提示!

【问题讨论】:

  • 把参数里的:ticket_id改成:id,或者把路由改成post 'tickets/:ticket_id/startnew' =&gt; 'tickets#startnew', as: :start_new
  • 你能运行rake routes | grep start并分享结果吗?

标签: ruby-on-rails url routing


【解决方案1】:

没有路线匹配 {:action=>"startnew", :controller=>"tickets", :ticket_id=>4} 缺少必需的键:[:id]

你有

post   'tickets/:id/startnew' => 'tickets#startnew', as: :start_new

这意味着它期望 :id 作为密钥,但您发送的是 :ticket_id。将您的链接更改为下面应该可以工作。

<%= link_to 'Book time', start_new_path(id: @ticket.id), method: :post, class: "button info block-shadow-info text-shadow", remote: true %>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多