javascriptruby-on-railsrjs

How to convert content of .js.rjs to .js.erb in rails


I used following lines in my create.js.rjs file. I just copied from internet and worked just fine.

page.select("#notice").each { |notice| notice.hide }

page.replace_html('cart', render(@cart))

page[:cart].visual_effect :blind_down if @cart.total_items == 1

page[:current_item].visual_effect :highlight,
                                  :startcolor => "#88ff88",
                                  :endcolor => "#114411"

But now I need to use these 2 lines code in create.js.erb file. Syntax for .rjs and .erb are not same. So what would be the code for .erb file for these 2 linces.


Solution

  • Try:

    # js.erb
    
    $("#notice").hide()
    $("#cart").html("<%= j render(@cart) %>")
    

    Update:

    Found this code in book Agile Web Development with Rails:

    js.rjs

    page[:cart].visual_effect :blind_down if @cart.total_items == 1
    
    page[:current_item].visual_effect :highlight,
                                      :startcolor => "#88ff88",
                                      :endcolor => "#114411"
    

    equivalent js.erb

    $('#current_item').css({'background-color':'#88ff88'}).
      animate({'background-color':'#114411'}, 1000);
    

    I kind of getting a feeling that the code you're following is discussed in the linked book in a great detail, so I would suggest taking a look at the book.