在视图中最好使用if条件?

时间:2014-01-21 09:20:59

标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-3.2 ruby-on-rails-3.1

方案/目标是:

当访客在主页上时,头部导航中的链接是:“别墅”,“公寓”,“B& B” 当访问者在国家页面上(例如:en / italy)时,头部导航中的链接是:“Villas Italy”,“Apartments Italy”,“B& B Italy” 当访客在区域页面上(例如:en / italy / tuscany)时,头部导航中的链接是:“Villas Tuscany”,“Apartments Tuscany”,“B& B Tuscany”

这是我的代码:

%ul.dropdown-menu.span3
- if @country.present? 
  %li
    %b
    = link_to "#{t('navigation.nav.houses_all')} #{@country.name}", country_houses_path(@country)
  %li.divider  
  %li.nav-header Thema's 
  - @country.tags.each do |a|
    %li
      #{link_to a.nav_content, tag_country_houses_path(a.country,  a.name).capitalize}
- if @region.present? && @country.present?
  %li
    %b
    = link_to "#{t('navigation.nav.houses_all')} #{@region.name}", country_region_houses_path(@country, @region)
  %li.divider  
  %li.nav-header Thema's
  - @region.tags.find_each(:conditions => "active_house = true") do |a|
    %li
      #{link_to a.nav_content, tag_country_region_houses_path(@country, @region, a.name)}
- else 
  %li
    %b
    = link_to "#{t('navigation.nav.houses_all')}", houses_path
  %li.divider

所以我想要实现的是当@region值设置时我想显示导航的一部分@country值设置我想显示另一部分。但是当我在区域页面(en / italy / tuscany)时,还会显示@country导航部分,因为@country也已设置。

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

如果你想要的只是在没有@country菜单时显示@region菜单,你可以试试elsif

%ul.dropdown-menu.span3
- if @region.present? && @country.present?
  %li
    %b
    = link_to "#{t('navigation.nav.houses_all')} #{@region.name}", country_region_houses_path(@country, @region)
  %li.divider  
  %li.nav-header Thema's
  - @region.tags.find_each(:conditions => "active_house = true") do |a|
    %li
      #{link_to a.nav_content, tag_country_region_houses_path(@country, @region, a.name)}
- elsif @country.present? 
  %li
    %b
    = link_to "#{t('navigation.nav.houses_all')} #{@country.name}", country_houses_path(@country)
  %li.divider  
  %li.nav-header Thema's 
  - @country.tags.each do |a|
    %li
      #{link_to a.nav_content, tag_country_houses_path(a.country,  a.name).capitalize}
- else 
  %li
    %b
    = link_to "#{t('navigation.nav.houses_all')}", houses_path
  %li.divider