2011-07-27 43 views
2

我目前正在使用一個使用handlebars.js模板的系統。然而,它在大多數情況下工作,問題是我們正在向它傳遞一個並不存在的屬性。Rails Json包含自定義值

最初我設置了它,以便模型通過方法傳遞字符串。另一位同事指出它有一種難聞的氣味,因爲函數只是簡單地返回一個固定的字符串,而沒有真正做任何邏輯。(我是新來的rails)。但是,我無法弄清楚如何以其他方式做到這一點。

我們終於設法通過導致屬性基本上嵌套的視圖將它附加到json。

問題是,當您嘗試向頁面添加另一個表單模板時,由於該字符串未嵌套在數據中,因此以此方式處理句柄現在失敗。我可以將它嵌入到數據中,但是我必須創建模板的重複項,因爲它在兩個地方使用。我還必須複製JavaScript。

我的問題是,按照「軌道方式」,簡化事情並返回模型簡單地傳遞固定字符串有多糟?這不是一個優雅的解決方案,但它絕對是更簡單的代碼方式。

下面是一些示例代碼:

<% fields_for :thing do |f| %> 
    <fieldset> 
     <div> 
     <div> 
      <input type="hidden" id="special_report_{{../type}}_attributes_{{sequence}}_id" name="special_report[{{../type}}_attributes][{{sequence}}][id]" value="{{id}}"/> 
      <input type="hidden" id="special_report_{{../type}}_attributes_{{sequence}}__destroy" name="special_report[{{../type}}_attributes][{{sequence}}][_destroy]" class="remove_flag" value="{{id}}"/> 
      <%= remove_child_link "remove", f %> 
     </div> 
     <p> 
      <span>Title: </span><br/> 
      <input type="text" id="special_report_{{../type}}_attributes_{{sequence}}_title" name="special_report[{{../type}}_attributes][{{sequence}}][title]" size="30" value="{{title}}"/>  
     </p> 
     <p> 
      <span>Date: </span><br /> 
      <input id="special_report_{{../type}}_attributes_{{sequence}}_event_date" name="special_report[{{../type}}_attributes][{{sequence}}][event_date]" size="30" type="text" value="{{event_date}}"/> 
     </p> 
     <p> 
      <br /> 
      <div> 
      <textarea id="special_report_{{../type}}_attributes_{{sequence}}_content" name="special_report[{{../type}}_attributes][{{sequence}}][content]">{{content}}</textarea> 
      </div> 
     </p> 
     </div> 
    </fieldset> 
    <% end %> 

的{{的指示車把變量。 這裏的視圖代碼

<script> 
    var updates_data = {update_content: $j.parseJSON('<%= @update_json %>'),"type":"updates"} 
    var highlights_data = { highlight_content: $j.parseJSON('<%= @highlight_json %>'),"type":"highlights"} 
</script> 

<% form_for(@foo) do |f| %> 
    <div> 
     <div> 
     <b>Updates:</b> 
     <p> 
      <%= add_child_link "Add update", :updates %> #custom wrapper for a link to the template 
     </p> 
     <div> 
      <script id="updates-template" type="text/x-handlebars-template"> 
      {{#each update_content}} 
       <%= new_child_fields_template(f, :updates)%> 
      {{/each}} 
      </script> 
      <script id="update-template" type="text/x-handlebars-template"> 
      <%= new_child_fields_template(f, :updates)%> 
      </script> 
     </div> 
     </div> 
     <div> 
     <b>Highlights:</b> 
     <p><%= add_child_link "Add highlight", :highlights %></p> 
     <div id="highlights"> 
      <script id="highlights-template" type="text/x-handlebars-template"> 
      {{#each highlight_content}} 
      <%= new_child_fields_template(f, :highlights)%> 
      {{/each}} 
      </script> 
     </div> 
     </div> 
    </div> 
<% end %> 

模型的方法只是

def type 
    "update" 
end 

我們從兩款車型的更新和亮點,他們都使用相同的表單模板拉動。

我在問,因爲這兩種技術對我來說都很新穎,我不確定哪種方法最好。提前致謝。

編輯或TLDR

基本上我問,是能夠更好地推動事情的看法,或者有一個方法,如:

def type 
    "update" 
end 

這種糟糕的情況?

+0

我很抱歉,但我不明白這個問題。顯然,我不是唯一一個。你應該更直。 – apneadiving

+0

是的,我很抱歉,真的很長。我大概可以把它總結得更好。 – Micharch54

回答

2

如果它們是兩個不同的類別(UpdateHighlight),您可以以某種方式在模板內部訪問model.class.name。 Ruby/Rails提供了一些內省,可以讓你獲得一個類的名字。

一旦你有了這個名字,你可以通過助手或其他東西來複制/大寫它。這會有幫助嗎?

另外,如果您的形式是基於模型的,而不是JSON數據發送到形式泛音和使用handlebars.js,你總是可以參考模型的形式是基於通過使用f.model.…

+0

我想到了這一點,我無法找到任何明確的語法。這很有道理,謝謝! – Micharch54

+0

Rosetta石頭髮現的+1 – apneadiving

+0

讓我知道該語法('model.class.name')是否是錯誤的;它不在我頭頂。 –