1

I’m trying to insert an ERB element into my page using JS, but the insertAdjacentHTML method doesn’t work and simply put my erb element as a string

I tried with raw, .html_safe and append(), but none of these work.

here's basically what I want to do

btn_plus.addEventListener('click', () => {
    btn_plus.insertAdjacentHTML('beforebegin', `<%= select_tag :rg1, options_from_collection_for_select(@users, 'id', 'email') %>`);
  })

and as a result it simply puts <%= select_tag :rg1, options_from_collection_for_select(@users, 'id', 'email') %> as a string on my page

What is the correct way to do it ?

1
  • rails tags are converted to html on server side so It won't work in js file because it will run on client side Commented Sep 23, 2019 at 15:49

2 Answers 2

1

in an .html.erb file, you can use an actionview helper to generate a string like this:

<%= javascript_tag do %>
    const foo = '<%= select_tag(:foo, raw("<option>bar</option><option>baz</option>")) %>'
    $('#put_it_here').append(foo)
<% end %>

you can use options_for_select too, but I didn't have anything handy to check it, so my example just uses raw options tags.

the resulting output in the browser is:

<script>
//<![CDATA[

  const foo = '<select name="foo" id="foo"><option>bish</option><option>bash</option></select>'
  $('#put_it_here').append(foo)

//]]>
</script>

and you can use the string 'foo' in your javascript expression to insert it dynamically where you wish, I've used jQuery here.

If you need to use the <select>...</select> html string in an included js file, you can declare it in the .html.erb file and later use it in the included file.

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

Comments

1

Abdul's comment is correct. Let me explain you in detail on how Rails rendering and JS works.

When you enter the URL on address bar, say http://url.com/articles/1, Rails recieves this request and looks for Articles#show action, then converts the show.html.erb file to an .html file(+ replaces dynamic stuff in this file) and then sends this HTML file to the browser. All this stuff is happening on the server side. Your in browser JS has nothing to do with it till now.

Now when you're browser recieves the HTML(+ CSS and JS files), the browser will render the HTML file and it will now execute the JS in browser. So now your btn_plus.addEventListener function is executed. It will do nothing to the rails part, it will just update the DOM. That's why when you inspect in dev tools you'll see

<%= select_tag :rg1, options_from_collection_for_select(@users, 'id', 'email') %>

rather than seeing an <select></select> tag.

You can do something like this. Include your <%= select_tag :rg1, options_from_collection_for_select(@users, 'id', 'email') %> where ever you want it in your .html.erb file. Then in your JS, you could do something like this:

const rg1 = document.getElementById('rg1');
const btn_plus = document.getElementById('btn_plus'); // change your ID here if it's different

// Hide the select tag
rg1.style.display = 'none';

// On btn click, show the select tag:
btn_plus.addEventListener('click', () => {
  rg1.style.display = 'inline-block'; // or `block` or anything you want
});

3 Comments

Thanks for the explications ! Actually I thought about the hidden/show method, but I want to add a new select every time the user clicks on a button
Ok, I think then you can do something similar to Les's Answer. Create a dynamic javascript_tag and append it everytime a user clicks the button.
yep, it worked. Thanks again for clarifying it for me !

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.