在回覆的創造,我想更新主板的:用線self.board.update_attributes(:toppid => reply_max.to_i + 1)
toppid列,但是這將返回NoMethodError in RepliesController#create undefined method 'update_attributes' for nil:NilClass
self.board.update_attributes返回未定義的方法`update_attributes方法
我怎樣才能正確地更新:toppid
專欄?
休息的我的代碼:
reply.rb:
class Reply < ActiveRecord::Base
belongs_to :board
belongs_to :post
after_create :set_pid
def set_pid
reply_max = self.post.replies.maximum(:pid)
board_max = self.board(:toppid)
if board_max.to_i > reply_max.to_i
self.update_attributes(:pid => board_max.to_i + 1)
self.board.update_attributes(:toppid => board_max.to_i + 1)
else
self.update_attributes(:pid => reply_max.to_i + 1)
self.board.update_attributes(:toppid => reply_max.to_i + 1)
end
end
end
replies_controller.rb:
class RepliesController < ApplicationController
def create
@board = Board.friendly.find(params[:board_id])
@post = @board.posts.friendly.find params[:post_id]
@reply = @post.replies.create(reply_params)
@post.touch
redirect_to @board
end
private
def reply_params
params.require(:reply).permit(:name, :email, :subject, :comment, :reply_file)
end
end
的routes.rb:
resources :boards, :path => '' do
resources :posts, :path => 'thread' do
resources :replies
任何機會是「未定義的方法」update_attributes「的NilClass」? (它有助於發佈完整的錯誤信息)。 –
@AndrewSchwartz是的,對不起,我也更新了它的帖子。 – aidiah
酷!所以如果它說'NilClass'(或者'nil')沒有'update_attributes'方法,那麼你認爲'self.board'是什麼意思,爲什麼? –