1

Im trying to generate html using rails select_tag and save it into a variable to be dynamically used. For some reason, below is causes this error:

Uncaught syntax error: Unexpected token ILLEGAL

Below is the problem code:

function addItemRowHTML(offsetidentifiervalue)
{
    $node = ' \
         <%= select_tag "currency",  
            options_for_select(Country.all.each_with_index.map {
            |country, index| 
                [country["currency_code"],
                 country["currency_code"]]},
                @invoice["data"]["currency"] ),
                :style => "width:120px"
        %> \
        ';

}

text_field_tag works though

$node =' <%= text_field_tag("amount", "10") %> ';

so how would i handle select_tag ? thanks

4
  • here are some of my failed example attempts: gist.github.com/axilaris/9615678 Commented Mar 18, 2014 at 8:54
  • 1
    check the generated javascript - it will likely have unescaped single quotes in it, therefore be invalid syntax. Commented Mar 18, 2014 at 8:54
  • you are right but its not unescaped single quotes, its probably unescaped multiline from the options generated gist.github.com/axilaris/9616215 Commented Mar 18, 2014 at 9:00
  • then it probably needs reformatting to insert \ on unescaped multiline Commented Mar 18, 2014 at 9:00

2 Answers 2

2
<% html_select_tag = select_tag( "currency",  
      options_for_select(Country.all.map(&:currency_code),
      @invoice["data"]["currency"] ),
      :style => "width:120px" ).gsub("\n", "\\n").gsub("'","\\'") %>

var $node = '<%= html_select_tag %>';

Someone already answered similar question here: https://stackoverflow.com/a/13631186/2134720

Sign up to request clarification or add additional context in comments.

8 Comments

i dont think that works. previous works... im not sure if you can assign html_select_tag, atleast that didnt work
Supposed to work. Tested it. What kind of error did it raise for you?
_form.html.erb:320: syntax error, unexpected tSTRING_DEND, expecting ')' options_for_select(Country.all.map(&:currency_code)}, ^
oops the curly brace close is not supposed to be there. Updated the answer. Let me know if it works now.
actually it does work with html_select_tag variable... but with the code below. do you want me to reedit the code from below, and i'll mark it as answered ? I'll just mark it right anyway, all depends probably how you want it, main thing is gsub replacement code. thats what is important.
|
0

this works

    $node = '<% select_tag("currency",  
            options_for_select(Country.all.each_with_index.map {
            |country, index| 
                [country["currency_code"],
                 country["currency_code"]]},
                @invoice["data"]["currency"] ),
                :style => "width:120px").gsub("\n", "\\n").gsub("'","\\'")
    %> ';

and this would be a better solution, there are less newlines to escape using raw

    $node = '<% raw select_tag("currency",  
            options_for_select(Country.all.each_with_index.map {
            |country, index| 
                [country["currency_code"],
                 country["currency_code"]]},
                @invoice["data"]["currency"] ),
                :style => "width:120px").gsub("\n", "\\n").gsub("'","\\'")
    %> ';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.