0
私はコントローラを行なうレールNoMethodError(未定義のメソッドが '52返信:整数):
エラー
NoMethodError (undefined method `replies' for 52:Integer):
app/controllers/replies_controller.rb:29:in `create'
返信フォーム
<%= form_with(model: @reply, url: [Comment.find(params[:id]), @reply]) do |form| %>
を受信コメントに対する回答を作成しようと -
を作成
def create
@reply = @comment.replies.create(reply_params).permit(:reply)
respond_to do |format|
if @comment.replies.save
format.html {redirect_to @reply, notice: 'Reply was successfully created.'}
format.json {render :show, status: :created, location: @reply}
else
format.html {render :new}
format.json {render json: @reply.errors, status: :unprocessable_entity}
end
end
end
def set_comment
@comment = Comment.find(reply_params[:post_id]).id
end
schema.rb
0123私が問題になるかもしれないと思う何routes.rbを
resources :comments do
resources :replies
end
はスキーマです。あなたは、IDから関連オブジェクトを取得しようとしている
あなたが正しいですが、今、私は別のエラーNoMethodError持つ(未定義のメソッドが '保存」#は<返信:: ActiveRecord_Associations_CollectionProxy:0x007f84e17be310を>):ライン: '@ comment.replies.save' – John
場合、私は参照してください。新しいオブジェクトの作成メソッドを使用しています。この場合、保存は必要ありません。とにかく、あなたはすべての応答コントローラを表示できますか? @ comment.replies.createは奇妙に見えます。 –
@John check Answer、私はコントローラがどのように見えるかの例を追加しました。 –