2016-09-18 204 views
0

我有多個模型,他們的索引視圖幾乎共享完全相同的代碼和佈局。如何與多個模型共享相同的視圖?

... /巧克力/ index.html.erb

<h1 class="col-sm-12 head">Index Bases</h1> 
<hr> 
<% @chocolates.each do |chocolate| %> 

    <%= link_to chocolate do %> 
    <%= set_img(chocolate) %> 
    <% end %> 

    <ul> 
    <li><%= chocolate.name %></li> 
    <li><%= chocolate.position %></li> 
    </ul> 
    <hr> 

    <div> 
    <%= link_to chocolate do %> 
     <span class="glyphicon glyphicon-eye-open"></span> 
    <% end %> 
    <%= link_to edit_chocolate_path(chocolate) do %> 
     <span class="glyphicon glyphicon-edit"></span> 
    <% end %> 
    <%= link_to basis, method: :delete, data: { confirm: 'Are you sure?' } do %> 
     <span class="glyphicon glyphicon-remove"></span> 
    <% end %> 
    </div> 

<% end %> 
<hr> 
<%= link_to '+ Add New Chocolate', new_chocolate_path %> 

... /糕點/ index.html.erb

<h1 class="col-sm-12 head">Index Bases</h1> 
<hr> 
<% @sweets.each do |sweet| %> 

    <%= link_to sweet do %> 
    <%= set_img(sweet) %> 
    <% end %> 

    <ul> 
    <li><%= sweet.name %></li> 
    <li><%= sweet.position %></li> 
    </ul> 
    <hr> 

    <div> 
    <%= link_to sweet do %> 
     <span class="glyphicon glyphicon-eye-open"></span> 
    <% end %> 
    <%= link_to edit_sweet_path(sweet) do %> 
     <span class="glyphicon glyphicon-edit"></span> 
    <% end %> 
    <%= link_to sweet, method: :delete, data: { confirm: 'Are you sure?' } do %> 
     <span class="glyphicon glyphicon-remove"></span> 
    <% end %> 
    </div> 

<% end %> 
<hr> 
<%= link_to '+ Add New Sweet', new_sweet_path %> 

還有更多的模式,共享相同的佈局,我認爲我不斷重複我的自我,所以我創建了一個共享部分,並在每個使用該佈局的視圖中呈現變量。

... /糕點/ index.html.erb

<% render 'shared/indexGrid', dist: @sweets%> 

... /視圖/共享/ _indexGrid.html.erb

<% title = dist.class.name.underscore.tr('_', ' ').pluralize.split.map(&:capitalize).join(' ') %> 
<% sing = dist.class.name.underscore %> 

<h1>Index <% title %></h1> 
<hr> 

<% dist.each do |sing| %> 

    <%= link_to sing do %> 
    <%= set_img(sing) %> 
    <% end %> 

    <ul> 
    <li><%= sing.name %></li> 
    <li><%= sing.position %></li> 
    </ul> 
    <hr> 

    <div> 
    <%= link_to sing do %> 
     <span class="glyphicon glyphicon-eye-open"></span> 
    <% end %> 

    <%= link_to send("edit_#{sing.class.name.underscore}_path", sing) do %> 
     <span class="glyphicon glyphicon-edit"></span> 
    <% end %> 

    <%= link_to sing, class: 'square red', method: :delete, data: { confirm: 'Are you sure?' } do %> 
     <span class="glyphicon glyphicon-remove"></span> 
    <% end %> 
    </div> 
<% end %> 
<hr> 
<%= link_to "+ Add New #{title}", send("new_#{sing.class.name.underscore}_path"), { class: 'btn btn-success btn-block'} %> 

,但它沒」噸似乎工作,因爲 - 我認爲 - dist.class.name deasnt返回我期待的價值,但返回"active_record/relation"。 我已經嘗試過與編輯視圖相同的方法,它與dist: @sweet

我曾經想過使用佈局,但它給我留下了與其他索引視圖非常相似的大塊代碼。

我分享我嘗試用你的時候,這個問題......

  • 這有什麼錯我的代碼,以及如何解決它?
  • 該方法是否被認爲是DRY代碼的良好實踐?如果不是的話當這些模型幾乎共享完全相同的代碼和佈局時,與多個模型共享相同視圖代碼的最佳方式是什麼?

回答

1

你可以嘗試model_name

<% title = dist.model_name.to_s.underscore.tr('_', ' ').pluralize.split.map(&:capitalize).join(' ') %> 

or better better

<% title = dist.model_name.human.pluralize.titleize %> 

也應該給你你想要的。

和清理的路線,你可以看看PolymorphicRoutes

http://api.rubyonrails.org/classes/ActionDispatch/Routing/PolymorphicRoutes.html

所以不是

link_to send("edit_#{sing.class.name.underscore}_path", sing) 

,你可以使用類似

link_to 'Edit, edit_polymorphic_path(sing) 

這應該清理很好地分享你的共享索引頁面。

你也可以看看創建一個title_for(例如)助手,它返回標題。清理它,但不是100%需要。

- 第2部分 -

這是一個好主意嗎?也許?那是不是答案。有很多因素。所有這些模型總是完全一樣的可能性有多大?稍後再做一個不同的工作會有多難?如果我有一堆模型基本相同,總是會有相同的索引頁面,我可能會考慮做類似的事情。自動生成視圖等的引擎/寶石/插件使用了同樣的想法。所以我猜如果他們總是會一樣的,而且你總是希望保持它看起來一樣,那麼像這樣的事情是完全沒問題的。

對於看起來像@sweets之類的部分集合,它不是您的模型的單個實例。這可能是一個ActiveRelation。所以從active_model調用model_name在這裏不會有太大的幫助。如果你看部分的導軌指南,你會看到render @collection。這基本上只是對集合中的每個對象進行迭代,爲您查看model_name,並將其傳遞給其相應的局部渲染。

所以如果你有一個@sweets的集合,其中有一堆Sweet對象,軌道將迭代集合。然後渲染_sweet.html.erb(遵循尋找哪個部分的常規規則)。有了這個,你可以調整你的<% dist.each do |sing| %>塊爲<%=render dist%>,然後只是確保每個模型有一個局部的,它可以使他們都有所不同

+0

感謝男人的偉大答案,現在工作得很好,但我的第二部分問題仍然沒有答案,那麼你認爲這是做這種事情的最好方法嗎?你認爲在模型之間分享觀點是正確的 - 性能明智的或如此。我在查看'Partial'文檔,並且發現了一些名爲'collection'的東西,我懷疑這與這種情況有關。還有,當我運行'@ sweets.model_name'時,它返回了一個包含很多值的散列,如命名的路徑和其他實例變量我也懷疑它可以幫助。 –

+0

增加了一些更多的信息,這將有助於... – Doon

+0

謝謝你的男人,最後一段很有幫助 –

0

一個簡單的解決方案是使用諧音包括class_name作爲參數

<% render 'shared/indexGrid', dist: @sweets, class_name = "Sweets"%> 

或添加什麼東西可以讓你認識到這是源....

相關問題