Tyler
Tyler

Reputation: 2386

ruby on rails no method error due to routes

I am getting a no method error when trying to display a destroy link.

Here's the code to my view

   <% @followed_locations.each do |followed_location| %>
      <tr>
        <td><%= followed_location.user_id %></td>
        <td><%= followed_location.location_id %></td>
        <td><%= link_to 'Show', api_v1_followed_location_path(followed_location) %></td>
        <td><%= link_to 'Edit', edit_api_v1_followed_location_path(followed_location) %></td>
        <td><%= link_to 'Destroy', followed_location, method: :delete, data: { confirm: 'Are you sure?' } %></td>
      </tr>
    <% end %>

and here's my code for the routes

namespace :api do
  namespace :v1 do
    resources :followed_locations do
      collection do
        post "by_user_id"
        post "by_location_id"
      end
    end
  end
end

I'm guessing it has something to do with the namespaces in my routes but i'm unsure how to fix it and I can't seem to find anything online about this. Thanks for any help!

Upvotes: 1

Views: 127

Answers (2)

Alexander
Alexander

Reputation: 2568

Try this one:

<%= link_to "Destory", api_v1_followed_location_path(followed_location), method: :delete, data: { confirm: 'Are you sure?' } %>

Upvotes: 1

Nikita Singh
Nikita Singh

Reputation: 370

Since if you have namspaces api and v1, you will need to include in the path as well. So it should be like:

<%= link_to 'Destroy', api_v1_followed_location, method: :delete, data: { confirm: 'Are you sure?' } %>

EDIT: Code wasn't showing up, fixed now

Upvotes: 1

Related Questions