jrocc
jrocc

Reputation: 1346

Add rails image_tag in jQuery

I was wondering is there a way to add a rails image_tag dynamically with jQuery.

$(targetPlayerElement).html("<%= image_tag 'User.find_by_id(27).profile_picture'%>");

This is what i tried so far.

Thank you for all the help.

Upvotes: 1

Views: 358

Answers (1)

Sebasti&#225;n Palma
Sebasti&#225;n Palma

Reputation: 33420

You can try with:

$(targetPlayerElement).html('<%= image_tag User.find_by_id(27).profile_picture %>');

That must look like:

$(targetPlayerElement).html('<img src="src" alt="46" />')

If you get an Uncaught SyntaxError: missing ) after argument list error in the console then is because the image_tag is inside double quotes, and the src of the image_tag will be also enclosed in double quotes.

There's no need to quote the result of User.find_by_id(27).profile_picture if it's a string, will be already quoted.

Which version of Rails? To use find instead find_by_id.

Also, use single quotes to enclose what image_tag prints.

Upvotes: 1

Related Questions