没有路由匹配缺少必需的密钥

时间:2018-04-14 12:09:57

标签: ruby-on-rails ruby routes

我是Ruy on Rails的新手,我得到这个没有路由匹配缺少必需的密钥:[:id]错误,我不知道如何解决它。我已经尝试过类似于我的解决方案,但没有任何作用。

以下是我的模特

this.Enabled = true;
this.Cursor = Cursors.WaitCursor;
.....

我的路线

class Hall < ActiveRecord::Base
        belongs_to :user
        has_many :fields
        has_many :comments
        end


class Comment < ActiveRecord::Base
      belongs_to :hall
      belongs_to :user
end

class User < ActiveRecord::Base      
    has_many :halls
    has_many :comments
end

和_comment.html.erb文件中的错误是

  devise_for :users
  resources :halls do
  resources :fields 
  resources :comments
  end

这是我得到的错误

<p><%=comment.content %></p>

<%= link_to "Edit", edit_hall_comment_path(comment.hall, comment) %>  <-- error

1 个答案:

答案 0 :(得分:0)

您的错误显然是尝试访问没有ID的编辑路径。你可能想看看https://apidock.com/rails/ActionView/Helpers/UrlHelper/link_to_if

或者只是让链接有条件。如果未加载的对象上没有ID,则编辑路径助手不会起作用。

<%= link_to "Edit", edit_hall_comment_path(comment.hall, comment) if comment.id %>