0

I followed these instructions, but now I'm running into a problem. When I click my unsubscribe_url in the email, I get the following error:

No route matches [GET] "/newsletter_subscriptions/unsubscribe/b25a63f6e525deccd6bcd9e896c3321f"

This is how I setup my routes:

put 'newsletter_subscriptions/unsubscribe/:unsubscribe_hash' => 'newsletter_subscriptions#unsubscribe', :as => 'unsubscribe'
resources :newsletter_subscriptions

Why do I get a [GET] error message when I'm actually using put?

In my NewsletterSubscriptionsController I have

def unsubscribe
  @newsletter_subscription = NewsletterSubscription.find_by_unsubscribe_hash(params[:unsubscribe_hash])
  @newsletter_subscription.update_attribute(:active, false)
end

The link that should hit the unsubscribe action in my controller is:

<%= link_to 'Unsubscribe', unsubscribe_url(@unsubscribe_hash), method: :put %>

When I inspect the link with browser dev tools it looks correct to me:

<a rel="nofollow" data-method="put" href="http://localhost:3000/newsletter_subscriptions/unsubscribe/b25a63f6e525deccd6bcd9e896c3321f">Unsubscribe</a>

I wonder why the link doesn't hit the unsubscribe action of my controller. If anyone can give me a push in the right direction, that would be wonderful. Thanks a lot!

  • How are you making the request? Is it a PUT request? – Dave Newton Dec 09 '19 at 14:55
  • 1
    `<%= link_to 'Unsubscribe', unsubscribe_url(@unsubscribe_hash), method: :put %>` – Ernest Mistiaen Dec 09 '19 at 15:12
  • looking in the log, can you determine if the incoming requested action is being interpreted as `put` or `get`? This will tell you whether the problem is in the view or the server. – Les Nightingill Dec 09 '19 at 17:21
  • My server log says this: `Started GET "/newsletter_subscriptions/unsubscribe/b25a63f6e525deccd6bcd9e896c3321f" for 127.0.0.1 at 2019-12-09 16:12:14 +0100 ActionController::RoutingError (No route matches [GET] "/newsletter_subscriptions/unsubscribe/b25a63f6e525deccd6bcd9e896c3321f"):` – Ernest Mistiaen Dec 09 '19 at 18:38
  • There should be a parameter, introduced by the link_to helper, which is a Rails hack to implement the `put` http verb. The parameter name is `_method` and the value should be `put`. Do you see this parameter a) in the params received at the server, or b) in the request sent from the browser (using browser's dev tools). – Les Nightingill Dec 09 '19 at 21:18
  • When you look at what the `link_to` ActionView helper renders on the page, it should be an html form. Within that form there should be a hidden field named "_method" with value "put". Do you see that form, and the hidden field? – Les Nightingill Dec 09 '19 at 21:19
  • I'll update my question with some more info from my server log and console – Ernest Mistiaen Dec 10 '19 at 07:45
  • OK... the form that ActionView generates to hack the "put" verb is generated "on the fly" and removed immediately after it's sent. I would guess that the problem you're having is in the javascript that Rails adds to the page. If you're using jquery-rails, then line 215 here: https://github.com/rails/jquery-ujs/blob/master/src/rails.js is where the form is created. Or if it's just vanilla Rails unubtrusive javascript (UJS) there's something similar. You need to dig into the js to see why it's not intercepting the request and sending the `_method=put` query attribute. Is the js on the page? – Les Nightingill Dec 10 '19 at 18:33
  • evidently the browser is just responding to the clicked link, and the Rails javscript is not being invoked. – Les Nightingill Dec 10 '19 at 18:35

0 Answers0