2017-09-29 1 views
0

基本的なRails MVCのコメントを編集/削除できませんが、user_idがforeignであるため、comments_controllerがuser_idを検索してcomments_idを検索しないという問題があります。キー。私の前提は、Comment.find(params [:id])がcomments_idにつながることですが、これはnpotの場合です。Railsアプリケーションでcomments_idをコントローラに渡す

これは私のcomments_controllerの最後の部分である:

def edit 
    @comment = Comment.find(params[:id]) 
    @user = current_user 
end 

def update 
    @comment = Comment.find(params[:id]) 
    @user = current_user 
    @comment.update(comment_params) 
    redirect_to @comment 
end 

def destroy 
    @user = current_user 
    @comment = Comment.find(params[:id]) 
    @comment.destroy 
    redirect_to comments_path 
end 

private 
def comment_params 
    params.require(:comment).permit(:user_id, :location, :title, :body) 
end 

私はこのような外観を削除/編集しようとしているビューのコメント:提供何かアドバイスのため

 <% @user.comments.each do |w| %> 
    <tr> 
     <td>Location:<%= w.location %></td>  
     <td>Title:<%= w.title %></td> 
     <td>Body:<%= w.body %></td> 
     <td><%= link_to 'Edit', edit_comment_path %></td> 
     <td><%= link_to 'Destroy', comment_path, 
      method: :delete, 
      data: { confirm: 'Are you sure?' } %></td><br> 
    </tr> 
    <% end %> 

感謝:-)

答えて

0

コメントを編集/破棄するときは、実際のコメントをlink_toヘルパーに渡す必要があります。 link_to 'Destroy', w, method: :delete, data: { confirm: 'Are you sure?' }のようなものがあります。

同様に編集:link_to 'Edit', edit_comment_path(w)

+0

はい!それはうまくいった - しかし、あなたが提案した2番目の方法だけが、パスのすぐ後ろに括弧で囲まれたw :-) – Robert

関連する問題