0

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?

4

3 に答える 3

2

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'"
于 2012-07-04T23:52:24.170 に答える
0
<input type="text" data-source='<%= @results.collect(&:name).to_json %>' data-provide="typeahead">

属性を囲む引用符はhtml5ではオプションです

于 2012-07-05T03:55:52.593 に答える
0

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.

于 2012-07-04T23:55:03.850 に答える