rony
rony

Reputation: 520

Undefined methods in Links#Show Rails

I want to make button Destroy in rails app, but it shows the comment

    undefined method `comment_path' for #<#<Class:0x007fe0566264c8>:0x007fe05a5df3f8>
    Did you mean?  font_path

While I don't have font_path, and when I get rid of this pseudocode, the problem is gone but I can't destroy comment.

  <% if comment.user == current_user -%>
            <%= link_to 'Destroy', comment, method: :delete, data: { confirm: 'Are you sure?' }, class: "btn btn-sm btn-default" %>
        <% end %>

If I get rid of this, the problem is gone, but I can't delete the comment. If I put the @ symbol before comment in 'Destroy', it's not the comment that will be deleted, but the post. Here's the full code:

<%= div_for(comment) do %>
<div class="comments_wrapper clearfix">
    <div class="pull-left">
        <p class="lead"><%= comment.body %></p>
        <p><small>Submitted <strong><%= time_ago_in_words(comment.created_at) %> ago</strong> by <%= comment.user.email %></small></p>
    </div>

    <div class="btn-group pull-right">
  <% if comment.user == current_user -%>
            <%= link_to 'Destroy', comment, method: :delete, data: { confirm: 'Are you sure?' }, class: "btn btn-sm btn-default" %>
        <% end %>
    </div>
</div>

This is my routes.rb code

    Rails.application.routes.draw do
  devise_for :users
  resources :links do
    member do
      put "like", to: "links#upvote"
      put "dislike", to: "links#downvote"
    end
    resources :comments
  end

  root to: "links#index"
  # The priority is based upon order of creation: first created -> highest priority.
  # See how all your routes lay out with "rake routes".

  # You can have the root of your site routed with "root"
  # root 'welcome#index'

  # Example of regular route:
  #   get 'products/:id' => 'catalog#view'

  # Example of named route that can be invoked with purchase_url(id: product.id)
  #   get 'products/:id/purchase' => 'catalog#purchase', as: :purchase

  # Example resource route (maps HTTP verbs to controller actions automatically):
  #   resources :products

  # Example resource route with options:
  #   resources :products do
  #     member do
  #       get 'short'
  #       post 'toggle'
  #     end
  #
  #     collection do
  #       get 'sold'
  #     end
  #   end

  # Example resource route with sub-resources:
  #   resources :products do
  #     resources :comments, :sales
  #     resource :seller
  #   end

  # Example resource route with more complex sub-resources:
  #   resources :products do
  #     #     resources :sales do
  #       get 'recent', on: :collection
  #     end
  #   end

  # Example resource route with concerns:
  #   concern :toggleable do
  #     post 'toggle'
  #   end
  #   resources :posts, concerns: :toggleable
  #   resources :photos, concerns: :toggleable

  # Example resource route within a namespace:
  #   namespace :admin do
  #     # Directs /admin/products/* to Admin::ProductsController
  #     # (app/controllers/admin/products_controller.rb)
  #     resources :products
  #   end
end

Thank you for your help!

Upvotes: 1

Views: 77

Answers (2)

Mariah
Mariah

Reputation: 62

Your comment path is nested under links. You would have to access it as:

<% if comment.user == current_user -%>
  <%= link_to 'Destroy', 
              [@link, comment],
              method: :delete,
              data: { confirm: 'Are you sure?' }, 
              class: "btn btn-sm btn-default" 
  %> 
<% end %>

Upvotes: 0

Shiva
Shiva

Reputation: 12524

This is because

<%= link_to 'Destroy', comment, ...

makes Rails to expect comment_path helper method to be present. But you have nested the resource comments inside links.

So this might help you

<%= link_to 'Destroy', link_comment_path(link_id: comment.link_id, id: comment.id), method: :delete,...

This answer might not be the best one; but it should work. Lets communicate

Upvotes: 1

Related Questions