Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 271
user3277633 Avatar asked Nov 07 '14 19:11

user3277633


1 Answers

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"
like image 176
Kasperi Avatar answered Oct 06 '22 08:10

Kasperi