2012-10-02 26 views
2

enter image description here我想就如何找到我的答案一些經驗豐富的老將建議。我知道我在我看來有太多的邏輯,而且我在重複自己,但我仍然試圖「通過做法學習」,這是關於我在什麼地方纔開始工作,我會學習如何當然,從那裏重構。查看邏輯與塊和條件軌道

我想迭代一個創建的物流,並且如果logistics_title(通過「X」,「Y」或「Z」形式傳遞)與「X」匹配,則在div中顯示X,否則不要「 t顯示任何東西。

<div class="container-fluid"> 
<div class="row-fluid"> 
<div class="span4 hero-unit"> 
    <% @logistics.each do |logistic| %> 
     <% if logistic.logistic_title = "Practice" %><br> 
     <%= logistic.logistic_title %> 
     <%= logistic.user.full_name %> 
     <%= logistic.content %><br> 
     <%= link_to time_ago_in_words(logistic.created_at) + " ago", logistic %> | 
     <%= link_to "Edit", edit_logistic_path(logistic) %> | 
     <%= link_to "Remove", logistic, method: :delete, data: { confirm: "Remove? This action cannot be undone."} %> 
    <% else %> 
     <%= puts "" %> 
    <% end %> 
    <% end %> 
</div> 
<div class="span4 hero-unit"> 
    <% @logistics.each do |logistic| %> 
    <% if logistic.logistic_title = "Game" %><br> 
     <%= logistic.logistic_title %> 
     <%= logistic.user.full_name %> 
     <%= logistic.content %><br> 
     <%= link_to time_ago_in_words(logistic.created_at) + " ago", logistic %> | 
     <%= link_to "Edit", edit_logistic_path(logistic) %> | 
     <%= link_to "Remove", logistic, method: :delete, data: { confirm: "Remove? This action cannot be undone."} %> 
    <% else %> 
    <%= puts "" %> 
    <% end %> 
    <% end %> 
</div> 
<div class="span4 hero-unit"> 
    <% @logistics.each do |logistic| %> 
    <% if logistic.logistic_title = "Etc." %><br> 
     <%= logistic.logistic_title %> 
     <%= logistic.user.full_name %> 
     <%= logistic.content %><br> 
     <%= link_to time_ago_in_words(logistic.created_at) + " ago", logistic %> | 
     <%= link_to "Edit", edit_logistic_path(logistic) %> | 
     <%= link_to "Remove", logistic, method: :delete, data: { confirm: "Remove? This action cannot be undone."} %> 
    <% else %> 
    <%= puts "" %> 
    <% end %> 
    <% end %> 
</div> 

我知道我需要寫一些輔助性的東西,但在正確的方向點幫助。 請溫柔一點,這是我學習的方式。

感謝您的關注和注意。

回答

3

你說得對,你的視圖中有很多重複的代碼和邏輯。如果我去這個做我自己,我可能會改變控制器,使您的@logistics實例變量被分爲三個實例變量的基礎上,標題:

@practice = @game = @etc = [] 
@logistics.each do |logistic| 
    if logistic.title == 'Practice' 
    @practice << logistic 
    elsif logistic.title == 'Game' 
    @game << logistic 
    elsif logistic.title == 'Etc.' 
    @etc << logistic 
    end 
end 

關於幫手,我想的link_to如果你正在編寫一個標準的Rails應用程序,並且你的控制器名稱與模型匹配,通常會爲你的助手創建logistic_edit_path助手,所以你不需要自己添加這些助手。

當您的意思是相等運算符時,請小心不要使用單個等於(=)。我認爲你的代碼應該看起來像這樣:

<div class="container-fluid"> 
<div class="row-fluid"> 
<div class="span4 hero-unit"> 
    <% @logistics.each do |logistic| %> 
     <% if logistic.logistic_title == "Practice" %> 
     <br> 
     <%= logistic.logistic_title %> 
     <%= logistic.user.full_name %> 
     <%= logistic.content %><br> 
     <%= link_to time_ago_in_words(logistic.created_at) + " ago", logistic %> | 
     <%= link_to "Edit", edit_logistic_path(logistic) %> | 
     <%= link_to "Remove", logistic, method: :delete, data: { confirm: "Remove? This action cannot be undone."} %> 
    <% else %> 
     <%= puts "" %> 
    <% end %> 
    <% end %> 
</div> 
<div class="span4 hero-unit"> 
    <% @logistics.each do |logistic| %> 
    <% if logistic.logistic_title == "Game" %><br> 
     <%= logistic.logistic_title %> 
     <%= logistic.user.full_name %> 
     <%= logistic.content %><br> 
     <%= link_to time_ago_in_words(logistic.created_at) + " ago", logistic %> | 
     <%= link_to "Edit", edit_logistic_path(logistic) %> | 
     <%= link_to "Remove", logistic, method: :delete, data: { confirm: "Remove? This action cannot be undone."} %> 
    <% else %> 
    <%= puts "" %> 
    <% end %> 
    <% end %> 
</div> 
<div class="span4 hero-unit"> 
    <% @logistics.each do |logistic| %> 
    <% if logistic.logistic_title == "Etc." %><br> 
     <%= logistic.logistic_title %> 
     <%= logistic.user.full_name %> 
     <%= logistic.content %><br> 
     <%= link_to time_ago_in_words(logistic.created_at) + " ago", logistic %> | 
     <%= link_to "Edit", edit_logistic_path(logistic) %> | 
     <%= link_to "Remove", logistic, method: :delete, data: { confirm: "Remove? This action cannot be undone."} %> 
    <% else %> 
    <%= puts "" %> 
    <% end %> 
    <% end %> 
</div> 
+0

非常感謝你。這是我尋找的確切答案,像你這樣的人讓我堅持了這麼久。 ==是什麼讓它工作的,我想你可以說v1,我想要的應用程序。現在從您答案的第一部分開始,我可以研究將代碼應用到「最佳實踐版」中再次感謝! –