현재 handlebars.js 템플릿을 사용하는 시스템에서 작업하고 있습니다. 그러나 대부분의 경우 작동하지만 문제는 실제로 존재하지 않는 속성을 전달하는 것입니다.Rails Json에는 사용자 정의 값이 포함됩니다.
원래 모델이 문자열을 메서드로 전달하도록 설정 했었습니다. 또 다른 동료는 함수가 단순히 고정 된 문자열 리터럴을 리턴하고 실제로 어떤 로직도 수행하지 않았기 때문에 악취가 난다고 지적했습니다. (나는 레일을 처음 사용합니다). 그러나, 나는 다른 방법으로 그것을하는 방법을 알아낼 수 없었다.
결국 본질적으로 중첩 된 속성으로 이끌어 낸보기를 통해 json에 추가 할 수있었습니다.
문제는 문자열이 데이터에 중첩되어 있지 않으므로 페이지에 다른 양식 서식 파일을 추가하려고하면이 방법으로 핸들 막대를 만들지 못하는 것입니다. 데이터에 중첩 할 수는 있지만 두 곳에서 사용되고 있기 때문에 템플릿 복제본을 만들어야합니다. 나는 또한 자바 스크립트를 복제해야합니다.
제 질문은 "레일 방식"을 유지하는 것입니다. 고정 된 문자열을 전달하기 만하면 모델을 단순화하고 모델로 돌아갈 수 있습니다. 그것은 우아한 해결책은 아니지만 분명 코드 위주로 간단합니다.
<% 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 %>
은 {{년대는 핸들 변수를 나타냅니다
는 여기에 몇 가지 예제 코드입니다. 다음은 단순히
def type
"update"
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 %>
입니다.
두 가지 기술이 모두 내게 새롭고 어떤 방법이 가장 좋으 냐고 묻기 때문에 나는 묻습니다. 미리 감사드립니다.
은 기본적으로 내가 부탁 해요
편집 또는 TLDR,보기에 뭔가를 밀어 낫다이나 방법 같은 데 :
def type
"update"
end
이러한 나쁜 상황을?
미안하지만 질문을 이해할 수 없습니다. Apparntly, 나는 유일한 사람이 아니다. 당신은 더 똑바로되어야합니다. – apneadiving
네, 정말 길다는 것에 대해 사과드립니다.나는 아마 그것을 더 잘 요약 할 수 있었다. – Micharch54