ruby - How to display HTML in jQuery autocomplete results in Rails -
i populating set of results jquery ui autocomplete in rails:
<%= text_field_tag('user', "", :placeholder => 'enter user...', class: "users", data: {autocomplete_source: user.order(:lastname, :firstname).map { |u| {:label => getlabel(u.firstname, u.lastname).html_safe, :id => u.id} }}) %>
i trying display html in labels, instance bolding text, , display perhaps image in each row.
def getlabel(firstname, lastname) "<b>" + firstname + " " + lastname + "</b>" end
this renders plain text in results:
<b>john smith</b>
what best way labels rendered html? appreciated.
edit:
$('.users').autocomplete source: $('.users').data('autocomplete-source') select: (event, ui) -> $( ".users" ).val( ui.item.value ); $( "#user_id" ).val( ui.item.id ); $( "#select_user" ).submit();
html:
<input class="users" data-autocomplete-source="[{"label":"\u003cb\u003ejohn smith\u003c/b\u003e","id":4}]" id="user" name="user" placeholder="enter user..." type="text" value="" />
unless declare string html-safe, escaped you. safety mechanism.
the straight-forward method employ html_safe
on tags:
def get_label(firstname, lastname) [ '<b>', h([ firstname, lastname ].join(' '))), '</b>' ].join('').html_safe end
even better, can use helper methods construct tags you:
def get_label(firstname, lastname) content_tag('b', [ firstname, lastname ].join(' ')).html_safe end
the second approach better since you're not running risk of accidentally declaring name html_safe
.
Comments
Post a Comment