我觉得我错过了一些东西,但是我的Rails应用程序出现了一个错误,我甚至都没提到。这可能与Request和Comment模型之间的关系有关。这是错误:
ActiveRecord::UnknownAttributeError in RequestController#comment
unknown attribute: request_id
这是相关的代码;
# Making a comment on a request
def comment
comment = @request.comments.create(text: params[:text]) # Error on this line
comment.user = current_user
comment.save
request.rb的代码:
class Request < ActiveRecord::Base
belongs_to :user
has_many :comments
end
对于comment.rb:
class Comment < ActiveRecord::Base
validates_length_of :text, :minimum => 1, :maximum => 300
belongs_to :user
belongs_to :request
end
答案 0 :(得分:0)
您在创建评论表的迁移中可能缺少这种关系。如果这不在生产中,我建议首先回滚到迁移之前(即rake db:migrate rollback STEP=[a number]
),然后在注释迁移中添加t.references :request
,然后再迁移回来。