ruby-on-rails - 在 haml View 中重构条件

标签 ruby-on-rails ruby haml

除了可访问性标准不鼓励使用这一事实 指向当前页面的链接,我应该怎么做 重构以下 View 代码?

#navigation
  %ul.tabbed
    - if current_page?(new_profile_path)
      %li{:class => "current_page_item"}
        = link_to t("new_profile"), new_profile_path
    - else
      %li
        = link_to t("new_profile"), new_profile_path

    - if current_page?(profiles_path)
      %li{:class => "current_page_item"}
        = link_to t("profiles"), profiles_path
    - else
      %li
        = link_to t("profiles"), profiles_path
    ...

谢谢。

最佳答案

# helpers
def current_page_class(page)
  return :class => "current_page_item" if current_page?(page)
  return {}
end

-# Haml
#navigation
  %ul.tabbed
    %li{current_page_class(new_profile_path)}
      = link_to t("new_profile"), new_profile_path
    %li{current_page_class(profiles_path)}
      = link_to t("profiles"), profiles_path
    ...

关于ruby-on-rails - 在 haml View 中重构条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1367876/

相关文章:

ruby-on-rails - 在 Heroku/Ruby on Rails 上找不到 PNG

ruby-on-rails - 无法使用 Mobility 按 Rails 中翻译的列进行排序

ruby - 将Redis链接到Sinatra Docker容器的正确方法

ruby - 使用 String#end_with?() 但忽略大小写

javascript - 使用 Ruby/Sinatra 将数据传递到 Highcharts 的更好方法?

playframework - JHAML 和 Play Framework ?

ruby-on-rails - habtm 关系不支持 :dependent option

ruby-on-rails - 延迟发送邮件以延长页面加载时间

ruby-on-rails - Rails 3 tag_list.each

ruby-on-rails - 通过 foreach 中 View 中的第一个字母进行 ruby​​ 组集合