5

我很抱歉,如果這不符合良好的問題準則,但我希望它在課堂上與How to Manage CSS Explosion一樣好,並收到類似的幫助。如何管理視圖文件中的if-then爆炸?

我熟悉的一些基本觀點羅嗦緩解策略,如下列:

  • 不要重複自己
  • 使用諧音和佈局
    • 使用助手在適當情況下

      如果我在上面的列表中錯過了一些重要想法,請隨時提出建議。不過,我仍然認爲在我看來有幾個維度/自由度,導致很多if-then語句或至少三元塊。例如,在一些我目前有,我正在一個標題欄上的,其中當三個「大」變量視圖稱爲程序搞亂:

      • 無論用戶是管理員
      • 無論在用戶登錄
      • 是否正在瀏覽的網頁屬於它結束了看起來像這個爛攤子用戶或其他人

      <% content_for :subheader do %> 
          <div class="row"> 
          <% if @user %> 
           <% if @user == current_user %> 
           <%= link_to 'My programs', user_programs_path(current_user), :class => 'active' %> 
           <% else %> 
           <%= link_to "#{@user.username}'s programs", user_programs_path(@user), :class => 'active' %> 
           <% end %> 
           <%= link_to 'Browse all programs', programs_path %> 
          <% else %> 
           <% if current_user %> 
           <%= link_to 'My programs', user_programs_path(current_user) %> 
           <% end %> 
           <%= link_to 'Browse all programs', programs_path, :class => 'active' %> 
          <% end %> 
          <%= link_to 'New Program', new_program_path, :class => 'admin' if current_user.admin? %> 
          </div> 
          <% if @regions %> 
          <div class="row second"> 
           <%= link_to 'Regional program search', request.fullpath, :class => 'active' %> 
          </div> 
          <% end %> 
      <% end %> 
      

      醜。易讀易讀,但醜陋。一些建議?

      在體驗和新技術如LESS之間,我已經非常擅長減少我的CSS文件,但是我仍然遇到了MVC視圖的爆炸問題。

    +0

    這將是這些問題之一,沒有人覺得遊戲不夠回答,因爲它是這樣一個自以爲是的問題..我期待着聽到人們想出了什麼雖然! – 2potatocakes 2011-02-17 00:36:21

    +0

    我真的希望有一個很好的方法來「解除」這個問題。如果您(以及其他人)有任何好的想法,請隨時編輯該問題。 – 2011-02-17 00:57:16

    回答

    6

    我會用助手和模型定義乾涸代碼:

    class User 
        def possesive 
        self == current_user ? 'My' : "#{username}'s" 
        end 
    end 
    
    module ...Helper 
        def user_program_link user 
        if user 
         link_to "#{user.possesive} programs", user_programs_path(user), :class => 'active' 
        elsif current_user 
         link_to 'My programs', user_programs_path(current_user) 
        end 
        end 
    end 
    

    然後,您可以簡化所有,如果爲user_program_path語句調用此:

    <%= user_program_link @user %> 
    

    這會降低你的查看代碼:

    <% content_for :subheader do %> 
        <div class="row"> 
        <%= user_program_link @user %> 
        <% if @user %> 
         <%= link_to 'Browse all programs', programs_path %> 
        <% else %> 
         <%= link_to 'Browse all programs', programs_path, :class => 'active' %> 
        <% end %> 
        <%= link_to 'New Program', new_program_path, :class => 'admin' if current_user.admin? %> 
        </div> 
        <% if @regions %> 
        <div class="row second"> 
         <%= link_to 'Regional program search', request.fullpath, :class => 'active' %> 
        </div> 
        <% end %> 
    <% end %> 
    

    繼續此過程以幹掉剩下的喲你的代碼也是如此。

    0

    重寫視圖代碼如下:

    <% content_for :subheader do %> 
        <div class="row"> 
        <% if @user || current_user %>  
         <%= link_to ((current_user == @user or @user.nil?) ? "My programs" : 
            "#{@user.username}'s programs"), 
            user_programs_path(@user || current_user), 
            :class => 'active' %> 
        <% end %> 
        <%= link_to 'Browse all programs', programs_path, 
          :class => (@user ? '' : 'active') %> 
        <%= link_to 'New Program', new_program_path, :class => 'admin' if current_user.admin? %> 
        </div> 
        <% if @regions %> 
        <div class="row second"> 
         <%= link_to 'Regional program search', request.fullpath, :class => 'active' %> 
        </div> 
        <% end %> 
    <% end %>