鉴于我在 routes.rb 中定义了以下路线
get "signup" => "users#new"
post "signup" => "users#create"
以及我的 erb View 中的以下条件
<%if current_page?(login_path) or current_page?(signup_path)%>
<p> NOW YOU'RE IN</p>
<% end %>
如何让 current_page 识别 GET 和 POST 路径?现在,无论何时在 users#create 中,p 标签都不会显示。我假设这是因为路由将/signup 定义为用户#new 的获取,而 post 则完全是另一回事。
我尝试了以下方法,但似乎不起作用
<%if current_page?(login_path) or current_page?(signup_path) or current_page?(controller: "users", action: "create")%>
<p> NOW YOU'RE IN</p>
<% end %>
编辑:我发帖的原因是因为如果注册表有错误,它会重定向回注册页面,但这次由于某种原因操作不再是新的,而是创建
最佳答案
current_page?
将始终在 POST 请求时返回 false。
从文档:
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
可能的解决方法:
if controller.controller_name == "users" && controller.action_name == "create"
或者
if request.path == "/signup"
关于ruby-on-rails - 无法获取 rails current_page?同时使用 GET 和 POST 的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26808793/