You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Devise gem访问登录页面时触发ActionController::UrlGenerationError错误

解决Devise登录页面的ActionController::UrlGenerationError错误

问题根源

访问Devise登录页面时,布局里的current_page?方法会继承当前页面的Devise命名空间,导致你写的controller: 'articles'被自动解析成devise/articles,但你的路由里根本没有这个命名空间下的Articles控制器,所以触发了路由匹配失败。

修复方案

有两种简单的修复方式,选其中一种即可:

方式1:在current_page?中明确指定无命名空间

修改_header.html.erb里的每个current_page?判断,添加namespace: nil参数,让Rails忽略当前的Devise命名空间,直接查找全局的Articles控制器:

<ul class="nav col-12 col-lg-auto me-lg-auto mb-2 justify-content-center mb-md-0">
  
 <%= link_to t('header.menu.home'), home_path, class: "nav-link px-2 text-white #{current_page?(controller: 'articles', action: 'home', namespace: nil) ? 'link-secondary' : 'link-dark'}" %>
 <%= link_to t('header.menu.delivery'), delivery_path, class: "nav-link px-2 text-white #{current_page?(controller: 'articles', action: 'delivery', namespace: nil) ? 'link-secondary' : 'link-dark'}" %>
 <%= link_to t('header.menu.contacts'), contacts_path, class: "nav-link px-2 text-white #{current_page?(controller: 'articles', action: 'contacts', namespace: nil) ? 'link-secondary' : 'link-dark'}" %>
 <%= link_to t('header.menu.answers'), answers_path, class: "nav-link px-2 text-white #{current_page?(controller: 'articles', action: 'answers', namespace: nil) ? 'link-secondary' : 'link-dark'}" %>
  <%= link_to t('header.menu.about'), about_path, class: "nav-link px-2 text-white #{current_page?(controller: 'articles', action: 'about', namespace: nil) ? 'link-secondary' : 'link-dark'}" %>
</ul>

<form class="col-12 col-lg-auto mb-3 mb-lg-0 me-lg-3" role="search">
  <input type="search" class="form-control form-control-dark text-bg-white" placeholder="Пошук..." aria-label="Search">
</form>

  <% if user_signed_in? %>
<div class="text-end">

  <%= button_to "Sign Out", destroy_user_session_path, data: { "turbo-method": :delete } %>
  
  <% else %>

  <%= link_to new_user_session_path do %>
  <button type="button" class="btn btn-outline-light me-2">Увійти</button>
  <% end %>
  <%= link_to new_user_registration_path do %>
  <button type="button" class="btn btn-warning">Реєстрац</button>
  <% end %>
   <% end %>
   
  
</div>
</div>
</div>

方式2:直接用路径辅助方法判断(更简洁)

把current_page?(controller: 'articles', action: 'xxx')替换成current_page?(xxx_path),这样更直观,也不会受命名空间影响:

<ul class="nav col-12 col-lg-auto me-lg-auto mb-2 justify-content-center mb-md-0">
  
 <%= link_to t('header.menu.home'), home_path, class: "nav-link px-2 text-white #{current_page?(home_path) ? 'link-secondary' : 'link-dark'}" %>
 <%= link_to t('header.menu.delivery'), delivery_path, class: "nav-link px-2 text-white #{current_page?(delivery_path) ? 'link-secondary' : 'link-dark'}" %>
 <%= link_to t('header.menu.contacts'), contacts_path, class: "nav-link px-2 text-white #{current_page?(contacts_path) ? 'link-secondary' : 'link-dark'}" %>
 <%= link_to t('header.menu.answers'), answers_path, class: "nav-link px-2 text-white #{current_page?(answers_path) ? 'link-secondary' : 'link-dark'}" %>
  <%= link_to t('header.menu.about'), about_path, class: "nav-link px-2 text-white #{current_page?(about_path) ? 'link-secondary' : 'link-dark'}" %>
</ul>

<form class="col-12 col-lg-auto mb-3 mb-lg-0 me-lg-3" role="search">
  <input type="search" class="form-control form-control-dark text-bg-white" placeholder="Пошук..." aria-label="Search">
</form>

  <% if user_signed_in? %>
<div class="text-end">

  <%= button_to "Sign Out", destroy_user_session_path, data: { "turbo-method": :delete } %>
  
  <% else %>

  <%= link_to new_user_session_path do %>
  <button type="button" class="btn btn-outline-light me-2">Увійти</button>
  <% end %>
  <%= link_to new_user_registration_path do %>
  <button type="button" class="btn btn-warning">Реєстрац</button>
  <% end %>
   <% end %>
   
  
</div>
</div>
</div>

验证

修改后重新访问登录页面,路由错误应该会消失,同时导航栏的active状态判断依然正常工作。

内容的提问来源于stack exchange,提问作者Yura Bunt

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 02:29:59