Search code examples
ajaxslim-lang

how to make ajax answer for slim?


in destroy.js.erb it works

$("<%= escape_javascript(render @comment) %>").appendTo("#commentlist");

how to do it for slim? I have tried

$("= escape_javascript(render @comment)").appendTo("#commentlist");

and renamed to destroy.js.slim, but its not works


Solution

  • By default, Slim tries to convert your code into HTML markup, so your current code produces an error. You can check it by viewing http://<website_adress>/something/destroy.js

    To fix this, you have to use pipe symbol, here is excerpt from official documentation:

    The pipe tells Slim to just copy the line. It essentially escapes any processing. Each following line that is indented greater than the pipe is copied over.

    http://rdoc.info/gems/slim/frames

    Also, ruby embedding work a bit different for Slim, you have to use #{ruby code} instead of equal sign, if you embedding it into string.

    To sum things up this is how it should look like to work:

    |
       $("#{escape_javascript(render @comment)}").appendTo("#commentlist");
    

    This way you can add more lines without prepending code with pipe each time. Just to note, there is also shortcut for escape_javascript called simply j, so you could've used this code:

    |
       $("#{j(render @comment)}").appendTo("#commentlist");