【发布时间】:2018-04-14 12:09:57
【问题描述】:
我是 Ruy on Rails 的新手,我得到这个 No route matches missing required keys: [:id] 错误,我不知道如何修复它。我已经尝试过与我类似的解决方案,但没有任何效果。
这是我的模型
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
我的路线
devise_for :users
resources :halls do
resources :fields
resources :comments
end
以及错误所在的_comment.html.erb文件
<p><%=comment.content %></p>
<%= link_to "Edit", edit_hall_comment_path(comment.hall, comment) %> <-- error
这是我得到的错误
No route matches {:action=>"edit", :controller=>"comments", :hall_id=>"1", :id=>nil} missing required keys: [:id]
【问题讨论】:
-
看起来您正在尝试
edit一个尚未保存到数据库的对象?你不能edit在数据库中持久化之前的东西。 -
感谢您的回答,但我认为这里不是这种情况,因为如果我删除导致错误的行,我可以发表评论,然后在大厅展示页面中呈现。
-
你在循环所有的 cmets 对吗?你能发布整个循环吗?
-
如果您发表评论,那么这保存它。您的错误是因为评论尚未保存(即
comment.id == nil),并且您正在尝试渲染路线以对其进行编辑。 Rails 无法生成路径来编辑尚不存在的内容。 -
在没有看到更多上下文的情况下,我不确定您在这里实际尝试做什么。也许
_comment.html.erb被用于多种用途,其中一些没有意义?
标签: ruby-on-rails ruby routes