Leahcim
Leahcim

Reputation: 41929

why isn't destroy link working from nested page

I have a Rails app that lets users sign up for a pro membership. If they want to cancel it, they can click this link, which triggers the destroy action on the promembers_controller.rb

<%= link_to "Cancel my account", promember_path(@user), :data => { :confirm => "Are you sure?" }, :method => :delete, :class => 'btn btn-mini' %>

I've tried to use this link on two different pages and it only works from one of them, namely the user's profile page

  http://localhost:3000/lawyer_profiles/22-user-name

However, I don't want the cancel subscription link on that page.

If I put the link on the 'show' page of the nested promembers_controller, like this

   http://localhost:3000/lawyer_profiles/22-user-name/promembers/40

or if I put it on the edit page like this

http://localhost:3000/lawyer_profiles/22-user-name/promembers/37/edit

then I get this routing error:

No route matches [DELETE] "/promembers"

I don't understand why it would make a difference where I put that link as long as I have a route for it, which I do

  resources :promembers do

     member { post :update_card }

  end 

I also have an action named 'destroy' in the promembers_controller.rb

It might be important to note that, in addition to the un-nested :promembers resource shown above, my routes.rb file also has :promembers nested like this.

  resources :lawyer_profiles do   
     resources :promembers
  end

(I'm not sure if having the same resource nested and un-nested could influence link behavior.)

Can you explain why the link isn't working from the nested pages?

Upvotes: 0

Views: 37

Answers (1)

Alex Peachey
Alex Peachey

Reputation: 4686

You are building your link with a path helper: promember_path(@user) and the @user variable is being set in the controller action for the page that is working.

On the pages that are not working, you are likely not setting the @user variable and so the path helper is being passed nil.

It is true you have a route for [delete] /promembers/:id but you don't have one for [delete] /promembers which is what is generated when you pass a nil into the helper. This causes the no route error.

Upvotes: 1

Related Questions