2017-11-25 6 views
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から関連オブジェクトを取得しようとしている

答えて

0

その応答テーブルに真:コメント、インデックス::真、FOREIGN_KEY

@comment = Comment.find(reply_params[:post_id]).id 

あなたが必要私はt.referencesを持っている同様の例を見てきました 目的:

def set_comment 
    @comment = Comment.find(reply_params[:post_id]) 
end 

UPDATE

適切なコントローラの例:

def create 
    @comment = Comment.find(reply_params[:post_id]) #In fact, it should look like this - params[:comment_id] 
    @reply = @comment.replies.new(reply_params) 

    respond_to do |format| 
    if @reply.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 

private 

    def reply_params 
    params.require(:reply).permit(...) #Model attributes 
    end 
+0

あなたが正しいですが、今、私は別のエラーNoMethodError持つ(未定義のメソッドが '保存」#は<返信:: ActiveRecord_Associations_CollectionProxy:0x007f84e17be310を>):ライン: '@ comment.replies.save' – John

+0

場合、私は参照してください。新しいオブジェクトの作成メソッドを使用しています。この場合、保存は必要ありません。とにかく、あなたはすべての応答コントローラを表示できますか? @ comment.replies.createは奇妙に見えます。 –

+0

@John check Answer、私はコントローラがどのように見えるかの例を追加しました。 –

関連する問題