2017-09-29 113 views
0

我無法編輯/刪除基本Rails MVC中的註釋,但是我遇到的麻煩是comments_controller查找user_id而不是comments_id,因爲user_id是外部鍵。我的假設是,Comment.find(params [:id])會導致comments_id,但這不是情況。在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

是的!它的工作 - 但只有第二種方式,你的建議,括號中的w直接後面的路徑:-) – Robert