简体   繁体   English

Rails 4.2.5 link_to删除方法

[英]Rails 4.2.5 link_to delete method

I have problem with a nested delete method for a link_to helper. 我对link_to助手的嵌套删除方法有link_to

Here are my routes: 这是我的路线:

resources :restaurants, only: [:new, :show, :edit, :index,:create] do
  resources :reservations, only: [:new, :show, :edit, :index, :create]
  resources :reviews
end

Here is my review controllers action: 这是我的审核控制器操作:

def destroy
  @review = Review.find(params[:id])
  @review.destroy
end

and my code on user#show : 和我在user#show上的代码:

<div class="panel-body">
<h1> <%= pluralize(@user.reviews.count ,'review') %> from <%=      @user.name %> </h1>
    <% @user.reviews.order(created_at: :desc).each do |review| %>
    <ul>
      <li><em>Review for restaurant: </em><%= review.restaurant.name %></li>
      <em>Review comment: </em> <%= review.comment %></br>
      <%= link_to 'edit comment', edit_restaurant_review_path(review.restaurant_id, review.id) %>
      <%= link_to 'delete comment', restaurant_review_path( @user, review.id ) , method: :delete, data:{confirm:"are you sure you want to delete this review"} %>
    </ul>
    <% end %>
</div>

Here is my route: 这是我的路线:

restaurant_reviews GET    /restaurants/:restaurant_id/reviews(.:format)               reviews#index
                        POST   /restaurants/:restaurant_id/reviews(.:format)               reviews#create
  new_restaurant_review GET    /restaurants/:restaurant_id/reviews/new(.:format)           reviews#new
 edit_restaurant_review GET    /restaurants/:restaurant_id/reviews/:id/edit(.:format)      reviews#edit
      restaurant_review GET    /restaurants/:restaurant_id/reviews/:id(.:format)           reviews#show
                        PATCH  /restaurants/:restaurant_id/reviews/:id(.:format)           reviews#update
                        PUT    /restaurants/:restaurant_id/reviews/:id(.:format)           reviews#update
                        DELETE /restaurants/:restaurant_id/reviews/:id(.:format)           reviews#destroy

I can't seem to delete my reviews. 我似乎无法删除我的评论。 Am I passing in the wrong variables? 我是否传递了错误的变量? to 'restaurant_review_path'? 到“ restaurant_review_path”? My route seems to be right. 我的路线似乎正确。 as my edit link_to helper is working. 当我的编辑link_to助手正在工作时。

restaurant_review_path( @user, review.id ) is wrong. restaurant_review_path( @user, review.id )错误。 You're passing @user as the restaurant argument, which is going to produce a link with the wrong ID. 您将@user传递为restaurant参数,这将产生具有错误ID的链接。

You should be giving it a restaurant (or restaurant ID) and a review, not a user and a review id, just like you're doing on the previous line with the edit link. 您应该为其提供餐厅(或餐厅ID)和评论,而不是用户和评论ID,就像您在带有编辑链接的上一行中所做的一样。

restaurant_review_path(review.restaurant_id, review.id)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM