【问题标题】:Devise and current_page?设计和 current_page?
【发布时间】:2013-05-21 05:46:39
【问题描述】:

在我的 Rails 4 应用程序中,我使用 devise 对我的用户进行身份验证,并且我使用 HAML 和 application_helper.rb 设置了一个自定义导航栏创建器。帮助程序中的代码使用 current_page 来决定将“活动”类添加到 HTML 元素的位置,因为我使用的是 twitter 引导程序。现在我所说的一切都很好,例如当您转到站点的根目录时,栏中的按钮处于活动状态(按下)但是如果您单击栏中的另一个项目并转到诸如 /sig_in/ 之类的页面您被发送到该页面,但该页面的按钮未激活。我不确定这是设计问题还是红宝石问题。这是我的代码:

routes.rb

devise_for :users, :path => ''

_nav.html.haml

= navigation_link_to 'Home', root_path

/ Insert Additional Nav

= navigation_link_to 'Sign in', new_user_session_path

application_helper.rb

 def navigation_link_to(text, path)
    link_to text, path, class: "btn#{active_link(path)}"
  end

  def active_link(path)
    ' active' if current_page?(path)
  end

【问题讨论】:

    标签: ruby haml ruby-on-rails-4


    【解决方案1】:

    对我来说这很好用(我在 Rails 4 中使用 3.0.0.rc 设计 gem)

    仅在 routes.rb 中:

    devise_for :users
    

    我有一个_login.html.haml,它被插入到_navigation.html.haml。由 Devise 自动提供的路径助手。如果用户已经登录,则会出现不同的链接。

    %li=link_to 'Registration', new_user_registration_path
    %li=link_to 'Sign in', new_user_session_path unless user_signed_in?
    -if user_signed_in?
       %li=link_to 'Manage your registration', edit_user_registration_path
       %li=link_to 'Logout', destroy_user_session_path, method: :delete
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-27
      • 2023-03-12
      • 1970-01-01
      • 2017-02-02
      • 2014-10-18
      • 2015-01-04
      相关资源
      最近更新 更多