Philip
Philip

Reputation: 103

Rails 4.2.5 link_to delete method

I have problem with a nested delete method for a link_to helper.

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:

<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'? My route seems to be right. as my edit link_to helper is working.

Upvotes: 0

Views: 1018

Answers (1)

user229044
user229044

Reputation: 239581

restaurant_review_path( @user, review.id ) is wrong. You're passing @user as the restaurant argument, which is going to produce a link with the wrong 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.

restaurant_review_path(review.restaurant_id, review.id)

Upvotes: 3

Related Questions