2009-12-01 55 views
1

我只想在這裏輸出一個錨點。如果current_page在數組中,我會得到兩個(.html和-nf.html)。如果它不在數組中,則獲取與數組中的項目一樣多的錨點。我正在使用StaticMaticruby​​ - 如果字符串不包含在數組中

- if page_options[:current_index] < page_options[:total_index] && page_options[:current_index] > 0 

    // loop through the pre-flash array and if current page matches, add -nf 
    - page_options[:pre_flash] = ['string_one-nf', 'string_two-nf', 'string_three-nf'] 
    - page_options[:pre_flash].each do |each_string| 

     - if current_page.include? each_string 
      %li 
       %a{ :href => "page#{page_options[:current_index]}-nf.html", :class => "next" } 
        Next 
     - else 
      %li 
       %a{ :href => "page#{page_options[:current_index]}.html", :class => "next" } 
        Next 

回答

2
unless current_page.include? the_string 

編輯:

你可以打破各環路,如果你希望你的第一個結論是唯一的一個。 但現在這看起來有點奇怪,因爲你正在遍歷一個數組 並在第一個元素後面發生,無論發生什麼。 我能解決你的問題嗎?

options[:pre_flash] = ['string_one-nf', 'string_two-nf', 'string_three-nf'] 
page_options[:pre_flash].each do |each_string| 
    if current_page.include? each_string 
    %li 
    %a{ :href => "page#{page_options[:current_index]}-nf.html", :class => "next" } 
    # This is the last ancor 
    break 
    else 
    %li 
    %a{ :href => "page#{page_options[:current_index]}.html", :class => "next" } 
    # This is the last ancor 
    break 
    end 
end 
+0

對不起,你們 - 這個比我意識到的還多,只是做你推薦的東西,我得到了多次發生的除非或!代碼(在這種情況下,主播) –

+0

不,仍然得到重複的.html錨不幸的 –

+0

打破了伎倆,我完全明白 - 非常感謝 –

1

好吧,所以我想我們正在檢查page_options [:current_index]是否爲current_page的子字符串。

if page_options[:current_index] < page_options[:total_index] && page_options[:current_index] > 0 

found_item = false 

// loop through the pre-flash array and if current page matches, add -nf 
- page_options[:pre_flash] = ['string_one-nf', 'string_two-nf', 'string_three-nf'] 
- page_options[:pre_flash].each do |each_string| 

    - if current_page.include? each_string 
      found_item = true 
      %li 
        %a{ :href => "page#{page_options[:current_index]}-nf.html", :class => "next" } 
          Next 

# do whatever you need to get out of the staticmatic block... 

    - if !found_item 

      %li 
        %a{ :href => "page#{page_options[:current_index]}.html", :class => "next" } 

對不起 - 我誤解你在做什麼......以爲你在做一個包括?在一個數組中,但它是一個字符串... :-)

+0

這聽起來沿着正確的線條,雖然http://pastie.org/721159是給我一個'不能將字符串轉換爲數組'錯誤 –

+0

這個餡餅並不完全是我的意思 - 把第二個如果在塊外面...順便說一句 - 什麼類型是current_page?它是一個字符串? – daf

+0

current_page確實是一個字符串 –

相關問題