I’m currently working on a system that is using handlebars.js template’s. It’s working for the most part however, the issue is that we’re passing it an attribute that doesn’t really exist.
Originally I had it set up so that the model was passing the string through a method. Another co-worker pointed out that it had a bad smell since the function was simply returning a fixed string literal and not really doing any logic.(I’m new to rails). However, I couldn’t figure out how to do it any other way.
We finally managed to append it to the json through the view which has led to the attribute essentially being nested.
The problem is that doing it this way handlebars now fails when you try to add another form template to the page because the string isn’t nested in the data. I can nest it in the data but I’ll have to create duplicates of the template since it’s being used in two places. I’ll also have to duplicate the javascript.
My question is, keeping with “the rails way,” how bad would it be to simplify things and go back to the model simply passing the fixed string? It’s not an elegant solution but it is definitely simpler code-wise.
Here’s some sample code:
<% 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 %>
The {{‘s indicate a handlebar variable.
Here’s the view code
<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 %>
The method in the model was simply
def type
"update"
end
We’re pulling from two models an updates and a highlights and they both use the same form template.
I’m asking because both of these technologies are pretty new to me and I’m not sure which method is best. Thanks in advance.
Edit or TLDR
Essentially I’m asking, is it better to push something to the view or is having a method such as:
def type
"update"
end
Such a bad situation?
If they’re two different classes (
UpdateandHighlight) you may be able to accessmodel.class.nameinside your template somehow. Ruby/Rails offers some introspection that allows you to get the name of a class.Once you’ve got that name you can run it through a helper or something that pluralizes/capitalizes it. Would that help?
Also, if your forms are based on models, instead of sending in JSON data to the form partials and using handlebars.js, you can always reference the model that the form is based on by using
f.model.….