I'm trying to create an array of values within an input field. However, the values need to have quotes and I'm having a hard time with quotes canceling each other out.
Here is my code:
<input type="text" data-provide="typeahead"
data-source='[<% @results.each do |result| %>"<%= result['name'] %>",<% end %>]'>
My issue is that I have either two sets of "
or '
. When I try something like result[name]
without the quotes I get the error: undefined local variable or method `name'.
Updated as I need data-source to have the ''s and data inside to have the ""s
How can I get around this?
Try this
<input type="text" data-provide="typeahead" data-source="[<%= @results.map { |r| "'#{r['name']}'" }.join(',') %>]">
In IRB this is what happens
>> results = [{'name' => "abc"}, {'name' => "fds"}]
=> [{"name"=>"abc"}, {"name"=>"fds"}]
>> results.map { |r| "'#{r['name']}'" }.join(',')
=> "'abc','fds'"
You should re-write that as something like this:
<input type="text" data-provide="typeahead" data-source="[<%= @results.each {|result| result['name']} %>">
Note this code is not tested.
Essentially the result of evaluating the ruby block inside of the beestings should be the desired string - you shouldn't need to do any nesting of beestings.
<input type="text" data-source='<%= @results.collect(&:name).to_json %>' data-provide="typeahead">
html5中的属性引用是可选的
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.