2013-04-09 144 views
4

我很沮喪地使用Rails 3.2併爲Bootstrap modals做一個幫手。我不明白你什麼時候需要concat,而當你有時候我不知道標籤是否丟失,有時候我最終會得到一個包含before和ending標籤之間所有選項的散列。當我在任何帶do-end的內容標籤上使用concat時,所有地獄都會崩潰。所有我想要做的就是複製這個網站:深層嵌套的content_tag,concat和rails 3

<div id="stupid_modal" class="modal hide fade" tabindex="-1" data-width="760"> 
    <div class="modal-header"> 
     <button type="button" class="close" data-dismiss="modal" aria-hidden="true"><i class="fontello-icon-cancel-1"></i></button> 
     <h4>Modal header</h4> 
    </div> 
    <div class="modal-body"> 
     <div class="page-header"> 
      <p>Test header 1 2 3.</p> 
     </div> 
     <div class="row-fluid"> 

      content here... blah blah 

     </div> 
    </div> 
    <div class="modal-footer"> 
     <button type="button" data-dismiss="modal" class="btn">Close</button> 
     <button type="button" class="btn btn-green">Save changes</button> 
    </div> 
</div> 

我不能爲我的生命得到的模態頭中的H4按鈕得以順利實施。我也無法將頁眉顯示在模式主體中。

幫助我的是這樣的:

module ModalHelper 
def modal(css_id, header_text, hidden = true, options = {},&block) 
    class_text = "modal" 
    class_text += " hide fade" if hidden 
    content_tag(:div, :class => 'modal hide fade', :id => css_id, :style => ("display:none;" if hidden)) do 
     concat modal_header(header_text) 
     concat modal_body(&block) 
     concat modal_footer 
    end 
end 

def modal_button(link_text, href) 
    modal_caller link_text, href, :button 
end 

def modal_link(link_text, href) 
    modal_caller link_text, href 
end 

private 

def modal_caller(link_text, href, type = nil) 
    options = { :"data-toggle" => "modal" } 
    options.merge!({ :class => "btn" }) if type == :button 
    link_to link_text, "#" + href, options 
end 

def modal_header(header_text) 
    content_tag(:div, :class => 'modal-header') do 
     concat content_tag(:button,(content_tag(:i, :class => 'fontello-icon-cancel-1')),:class => 'close', :"data-dismiss" => 'modal', :"aria-hidden" => 'true') 
     concat content_tag(:h4, header_text) 
    end 
end 

def modal_body(page_header = "") 
    content_tag(:div, :class => 'modal-body') do 
     content_tag(:div, :class => 'page-header') do 
      concat content_tag(:p, page_header) 
     end 
     content_tag(:div, :class => 'row-fluid') do 
      yield 
     end 
    end  
end 

def modal_footer 
    content_tag(:div, :class => 'modal-footer') do 
     concat content_tag(:button, 'Close', type: "button", :class => 'btn btn-boo', :"data-dismiss" => 'modal') 
     concat content_tag(:button, 'Save', type: "button", class: 'btn btn-green') 
    end  
end 

和鏈接看起來是這樣的:

<%= modal_link "New Stupid Modal", "stupid_modal" %> 

而HTML模式是這樣的:

<%= modal('stupid_modal', 'Shouldnt this work?', submit: true, tabindex: '-1') do %> 
    <% render 'stupid_modal_partials/stupid_modal' %> 
<% end %> 

outp UT是這樣的:

<button aria-hidden="true" class="close" data-dismiss="modal"><i>{:class=&gt;&quot;fontello-icon-cancel-1&quot;}</i></button> 

看起來像這樣在頁面的源代碼:

<i>{:class=>"fontello-icon-cancel-1"}</i> 

更新:

改變modal_header到這個工程:

def modal_header(header_text) 
    content_tag(:div, :class => 'modal-header') do 
     concat content_tag(:button,(content_tag(:i, "",:class => 'fontello-icon-cancel-1')),:class => 'close', :"data-dismiss" => 'modal', :"aria-hidden" => 'true') 
     concat content_tag(:h4, header_text) 
    end 
end 

但這不:

def modal_header(header_text) 
    content_tag(:div, :class => 'modal-header') do 
     concat content_tag(:button,:class => 'close', :"data-dismiss" => 'modal', :"aria-hidden" => 'true') do 
      concat content_tag(:i, "",:class => 'fontello-icon-cancel-1') 
     end 
     concat content_tag(:h4, header_text) 
    end 
end 

哪個問題,wzup與concat?並且我錯過了一些東西 - 我還嘗試了空引號作爲按鈕的第二個參數content_tag

+0

而且我已經試過按鈕標記的content_tag do-end內的i標記而不是內聯。沒愛。 – 2013-04-09 06:20:10

+0

本文解釋'concat'和'capture'的用戶:http://thepugautomatic.com/2013/06/helpers/ – 2014-10-16 10:44:06

回答

10

您不需要永遠使用concat

每個Rails的助手返回一些HTML中的字符串吧:

tag(:br) # "<br>" 

所以你簡單的輔助方法,會是這樣:

# "<br>" 
def br 
    tag(:br) 
end 

如果你有HTML的多串只是總結起來:

# "<button>Close</button><button>Save</button>" 
def modal_buttons 
    content_tag(:button, "Close") + content_tag(:button, "Save") 
end 

請注意,您不能只調用它們,因爲它們不會修改視圖

# "<button>Save</button>" 
def modal_buttons 
    content_tag(:button, "Close") # this won't do anything 
    content_tag(:button, "Save") 
end 

對於塊相同的規則:

# "<div><button>Close</button><button>Save</button></div>" 
def modal_footer 
    content_tag(:div) do 
    # what block returns will be inside the div 
    content_tag(:button, "Close") + content_tag(:button, "Save") 
    end 
end 

def modal_body 
    content_tag(:div) do 
    modal_header + yield + modal_footer 
    end 
end 

作爲一個側面說明,只使用Rails的助手來構建整個觀點是不是真的自己預期的目的。他們應該是幫助你在動態的地方,靜態HTML是在ERB模板更好地完成。

+0

這很有幫助。會嘗試。我想到了modals的助手,並且使用主體部分是清理我的觀點的好方法。 – 2013-04-09 18:41:19