0

current_page? doesn't work with POST requests, but there is said to be a solution here.

I try using url_for like so:

current_page?(url_for(controller: 'pages', action: 'booking_confirm'))

# but..

ActionController::UrlGenerationError in Pages#index

No route matches {:action=>"booking_confirm", :controller=>"pages"}

For reference, here are the attempts that fail due to the request being POST

# 1
current_page?(url_for('/booking_confirm'))

# 2
current_page?('/booking_confirm')

Also, here's some interesting knowledge on the matter. I don't fully understand it, but it hints that current_page? may not be able to do what I need it to do, but I ask here in case someone has some ideas

stevec
  • 41,291
  • 27
  • 223
  • 311
  • Can you elaborate on the use case ? maybe there are other simple solutions to use, especially if this method is kinda unstable / unpredicatble – Maxence Dec 03 '20 at 20:04
  • I agree with @Maxence. I cannot understand when `current_page?` for a POST route would serve a purpose but the other issue is that the error you posted seems to suggest no route exists for that page and action. Can you please post the relevant routes if they exist? – engineersmnky Dec 03 '20 at 20:49

1 Answers1

0

This is not an answer to why current_page? doesn't play nice with POST to a route constructed via url_for, but I found a workaround in case it is useful to someone else:

I ultimately needed to get some HTML (a script tag) into the head of a document, so I put this in the head:

<%= yield :whatever_name_you_want %>

And this in the template/view:

<% content_for :whatever_name_you_want do %>
   <script src="https://js.stripe.com/v3/"></script>
<% end %>

And that solved the problem in 30 seconds, making <script src="https://js.stripe.com/v3/"></script> appear in that template (or any template where I put the code block). Answer from here

stevec
  • 41,291
  • 27
  • 223
  • 311
  • TBH I struggle to see a relationship with your original question but I am glad you could find a solution – Maxence Dec 04 '20 at 10:34