can't get rails current_page? method to work with both GET and POST
Asked Answered
R

1

5

Given I have the following routes defined in routes.rb

get "signup" => "users#new"
post "signup" => "users#create"

and the following condition in my erb view

<%if current_page?(login_path) or current_page?(signup_path)%>
        <p> NOW YOU'RE IN</p>
<% end %>

How do I get both the GET and POST path to be recognized by current_page? Now whenever it's in users#create, the p tag does not show. I am assuming it's because the route defined /signup as a get for users#new, and post is something else entirely.

I've tried the following, but it does not appear to work

<%if current_page?(login_path) or current_page?(signup_path) or current_page?(controller: "users", action: "create")%>
            <p> NOW YOU'RE IN</p>
    <% end %>

Edit: The reason why I have a post is because if the sign up form has an error, it will redirect back to the sign up page, but this time for some reason the action is no longer new, but create

Retinitis answered 7/11, 2014 at 19:38 Comment(0)
J
17

current_page? will always return false on POST request.

From documentation:

Let’s say we’re in the http://www.example.com/products action with method POST in case of invalid product.

current_page?(controller: 'product', action: 'index') 
# => false

Possible workarounds:

if controller.controller_name == "users" && controller.action_name == "create"

or

if request.path == "/signup"
Junker answered 8/11, 2014 at 0:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.